Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions chapters/01-sdk-first-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions chapters/02-streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
9 changes: 4 additions & 5 deletions chapters/03-repl-telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions chapters/05-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down