# From "hello" to agent, with curl

**New here? Start with `paste/`.** It's the same eight steps as single `curl` commands you copy and paste,
with no scripts and nothing to install. Come back here when you want code to do the pasting.

Build an AI agent in eight steps with `curl`, `jq` and about 60 lines of bash.
Each step is a small script that adds to the one before. `step8.sh` is the finished agent.

You need `curl`, `jq` (both ship with macOS; on Windows use WSL or Git Bash) and an API key.
No budget? `groq/` has the same eight scripts for Groq's free tier.

    cd curl
    export ANTHROPIC_API_KEY=sk-ant-...
    bash step1.sh

## The helpers

Step 1 is one raw `curl` call. From step 2 on, the scripts use `helpers.sh`, where each helper is a single
`curl` or `jq` command. Read it; there's no magic. `source helpers.sh` to use them by hand.

| Helper | What it does | Step |
|---|---|---|
| `fresh FILE` | start `convo.json` from a request file (`01-hello.json`, `02-tools.json`) | 2 |
| `say "..."` | add your message to `convo.json` | 2 |
| `ask` | send `convo.json`, save `reply.json`, print the text and any tool request | 1 |
| `remember` | add the model's reply to `convo.json` | 3 |
| `stop_reason` | why the model stopped: `end_turn`, or `tool_use` when it wants a tool | 5 |
| `tool_arg path` | an argument of the tool it asked for | 6 |
| `read_file PATH` | the tool itself | 6 |
| `answer` | send a tool's output back: `read_file notes.txt \| answer` | 6 |
| `history` | one line per message, to watch the conversation grow | 3 |

## The steps

| Script | What to type | What to notice |
|---|---|---|
| `step1.sh` | nothing | One request, one reply. The reply is a list of content blocks. |
| `step2.sh` | `my name is Sam`, then `what's my name?` | It forgets you. Each request stands alone. |
| `step3.sh` | the same two | `convo.json` is its whole memory, and you resend all of it every time. |
| `step4.sh` | `what's in notes.txt?` | Instructions say to look at files. It can't yet. |
| `step5.sh` | `what's in notes.txt?` | `stop_reason: tool_use`. It asks to run `read_file`. Nothing runs. |
| `step6.sh` | `what's in notes.txt?` | Your code runs the tool and sends the result back. One round only. |
| `step7.sh` | `which is longer, notes.txt or helpers.sh?` | The model keeps asking until it's done. Now it's an agent. |
| `step8.sh` | `summarize every file in this folder` | A step cap, and you approve each action. Say `n` to one. |

Press Enter on an empty line to quit.

## Be the loop

Before running steps 5 to 8, do one round by hand. You are the code:

    source helpers.sh
    fresh 02-tools.json
    say "what's in notes.txt?"
    ask
    remember
    read_file notes.txt | answer
    ask

The first `ask` gets a request for `read_file`. `remember` keeps that request in the conversation,
`read_file notes.txt | answer` sends the file back as its result, and the second `ask` gets the answer.
This works in bash or zsh.

Then `step7.sh` is that, in a `while` loop. Answer with `echo "The user declined." | answer` instead,
and that's step 8.

## When something goes wrong

- **`error: x-api-key header is required`**: `ANTHROPIC_API_KEY` isn't set in this shell.
- **An error about `tool_use` ids without `tool_result` blocks**: by hand, you ran `answer` or `say` without
  `remember` first. Every tool request must be in the conversation, directly followed by its result.
- **`reply.json has no tool call to answer`**: the last reply didn't ask for a tool.
- Want to see everything the API sent back? `jq . reply.json`.

`remember` copies blocks `ask` doesn't print: replies include `thinking` blocks (empty by default), and the API
expects them back unchanged during a tool loop. `02-tools.json` sets `"disable_parallel_tool_use": true` so each
reply asks for at most one tool, which keeps `answer` simple. Real agents run several at once.

## Next: the same loop in Python

`../steps/` has the same eight steps in Python with Anthropic's SDK (`../steps-groq/` for Groq), plus
`step9-router.py`, which routes each request to the right tools and model before the loop starts.
