Docs / How Agents Talk to Models
How Agents Talk to Models
An agent doesn't “use an AI.” It makes a stream of ordinary HTTP calls, each one stateless and metered by the token. This short course builds the whole picture from first principles — following one real task, a coding agent fixing an endpoint — from its very first request out to a full tool-using, cached, multi-turn session, then streaming, structured output, images, MCP, and errors. No prior Firstpass knowledge needed; the last module shows where it changes the bill.
POST /orders reject a request with no total field, and add a test.” You'll watch the actual HTTP requests it produces — one call becomes a conversation, then a tool loop, then a whole session — and see the bill build on a single thread.The story, in one picture
The core path is one arc: a single call is nearly free, but conversation compounds it, tools multiply it, and caching bends it back down — and a full session is the sum of all of it. Each module below adds (or removes) one layer of this bill.
Part A · The core path
The main road — the path our one task actually walks, from a single request to a full session.
One request, one response →
A model call is one stateless POST — meet usage, the meter.
The token meter →
Why output costs ~5× input, and the strong tier 15–75× the cheap one.
Statelessness & multi-turn →
The client resends the whole chat every turn; input cost compounds.
Tool calls →
One task becomes many billed round-trips through a loop.
Prompt caching →
Re-price the repeated prefix from full rate to ~0.1×.
Three client shapes →
Interactive, headless, SDK — who drives the loop, and where the bill goes.
A Claude Code session →
Every earlier module at once, over one real session.
Part A2 · Beyond the core path
The turns a real agent takes off the happy path — each a dedicated module, take them in any order.
Streaming →
The same answer, token by token — delivery changes, cost doesn't.
Structured outputs & JSON →
Machine-readable output, reliably — schemas and the truncation trap.
Images & multimodal →
Image blocks bill as input tokens, by resolution.
MCP, in depth →
Transports, the three primitives, and the cost of more tools.
Errors, rate limits & retries →
429 / 5xx, backoff, idempotency — and failed-but-billed calls.
Part B · Where Firstpass fits