Build it yourself

The agent, one step at a time

This follows Thorsten Ball's tutorial, which is excellent and assumes you already know where things go in a Go file. This version shows the whole of main.go after every step, marks the lines to add, and says in words where each one belongs. Each explanation is written so a five-year-old could follow the idea.

Step 0 of 7

Make an empty project

#eli5

Before you can build with blocks you need a table to build on. These four commands make the table: a folder, a file that tells Go this folder is a project, an empty main.go to write in, and your key so Claude knows the bill goes to you.

Youtype a messagemain.goyour Go programkeeps the pile of notesClaudereads, repliesyourmessagethe answerthe wholeconversationwordschecks: size, rate, daily budgetruns the tool, sends back the resultsandbox: the only files it can touchread_fileopen a filelist_filessee what is thereedit_filechange some text
Nothing is built yet. This is the finished picture, shown faintly.

What you add

  • Nothing in main.go yet. You only run commands in your terminal.
  • The folder name does not matter. The module name after go mod init does not matter either; the tutorial uses agent.

Type these in your terminal

Terminal
mkdir code-editing-agent
cd code-editing-agent
go mod init agent
touch main.go
export ANTHROPIC_API_KEY="paste your key here"

Watch out. The export line only lasts for the terminal window you typed it in. If you open a new window, type it again.

main.gothe finished file, 317 lines · which step adds each part
  1. 3–15import
  2. 17–21type Agent
  3. 23–33func NewAgent
  4. 35–52func main
  5. 54–97func (a *Agent) Run
  6. 99–119func (a *Agent) executeTool
  7. 121–140func (a *Agent) runInference
  8. 142–147type ToolDefinition
  9. 149–154var ReadFileDefinition
  10. 156–158type ReadFileInput
  11. 160–160var ReadFileInputSchema
  12. 162–174func ReadFile
  13. 176–188func GenerateSchema
  14. 190–195var ListFilesDefinition
  15. 197–199type ListFilesInput
  16. 201–201var ListFilesInputSchema
  17. 203–246func ListFiles
  18. 248–258var EditFileDefinition
  19. 260–264type EditFileInput
  20. 266–266var EditFileInputSchema
  21. 268–300func EditFile
  22. 302–317func createNewFile

Everything goes in one file, top to bottom in this order. Each step shows the exact lines.