diff --git a/chapters/01-sdk-first-request.md b/chapters/01-sdk-first-request.md index a7b130b..508d800 100644 --- a/chapters/01-sdk-first-request.md +++ b/chapters/01-sdk-first-request.md @@ -6,18 +6,18 @@ Everything in this chapter is a single round trip: you send a JSON body, you get ```text your code - | - v - +-------------------------------------------------+ - | REQUEST POST /v1/messages | - | model, max_tokens, messages, system? | - +-------------------------------------------------+ - | - v - +-------------------------------------------------+ - | RESPONSE Message | - | content[], stop_reason, usage | - +-------------------------------------------------+ + │ + ▼ + ┌─────────────────────────────────────────────────┐ + │ REQUEST POST /v1/messages │ + │ model, max_tokens, messages, system? │ + └─────────────────────────────────────────────────┘ + │ + ▼ + ┌─────────────────────────────────────────────────┐ + │ RESPONSE Message │ + │ content[], stop_reason, usage │ + └─────────────────────────────────────────────────┘ ``` Prerequisites: Bun installed and your API credentials in a `.env` file at the project root (the next section explains which variables). No earlier chapters required. diff --git a/chapters/02-streaming.md b/chapters/02-streaming.md index fe62be4..428566f 100644 --- a/chapters/02-streaming.md +++ b/chapters/02-streaming.md @@ -28,20 +28,20 @@ The streaming and blocking paths converge on one shape: a fully typed `Anthropic ```text message_start usage.input_tokens - | - v + │ + ▼ content_block_start index, type (e.g. text) - | - v + │ + ▼ content_block_delta text_delta / input_json_delta (repeats) - | - v + │ + ▼ content_block_stop index - | - v + │ + ▼ message_delta stop_reason + usage.output_tokens - | - v + │ + ▼ message_stop ``` diff --git a/chapters/03-repl-telegram.md b/chapters/03-repl-telegram.md index 415ce64..24ddcc5 100644 --- a/chapters/03-repl-telegram.md +++ b/chapters/03-repl-telegram.md @@ -3,11 +3,10 @@ 💬 So far each script asked Claude one fixed question and exited. Now you'll hand the keyboard to a real person. A coding agent is, at heart, a loop: read input, build messages, call Claude, emit output, repeat. The Claude call stays the same; only the I/O around it changes - so in this chapter you wrap that one call in two different frontends, a terminal REPL and a Telegram bot. ```text - read input -> build messages -> call Claude -> emit output -> repeat - | - v - stop_reason 'end_turn' - ends the turn + read input → build messages → call Claude → emit output → repeat + │ + ▼ + stop_reason 'end_turn' ends the turn ``` You already have `new Anthropic()`, the env vars, and the content-array narrowing from Chapter 1, plus `stream()`, `.on('text')`, and `finalMessage()` from Chapter 2. Here you only add the input/output plumbing. The one new env var is `TELEGRAM_BOT_TOKEN`, which lives in `.env` next to your Anthropic credentials - Bun auto-loads it, and as always the key comes from the environment; never hardcode it. diff --git a/chapters/05-tools.md b/chapters/05-tools.md index 2b3f9dc..5229125 100644 --- a/chapters/05-tools.md +++ b/chapters/05-tools.md @@ -27,8 +27,19 @@ Notice that you find the call by narrowing `response.content` to the block whose Catching the call is half the round-trip; now you close it. The flow is a fixed five-step dance, and it is worth holding the whole shape in your head before you read the code: ```text -create(tools) -> stop_reason 'tool_use' -> run the tool locally - -> create again with a tool_result -> final text answer + create() with tools + │ + ▼ + stop_reason: 'tool_use' + │ + ▼ + run the tool locally + │ + ▼ + create() with the tool_result + │ + ▼ + final text answer ``` When `stop_reason === 'tool_use'`, you append the assistant's content **verbatim** as an assistant turn, run the tool yourself, then send a new `user` turn whose `content` is a `tool_result` block - an `Anthropic.ToolResultBlockParam` that echoes the `tool_use_id` exactly. That echo is the rule that binds request to answer: every `tool_use` block in a turn needs a matching `tool_result`, or the next `create` call rejects the array.