CCDV-F · Study Guide

← Domain 5: Model Selection and Optimization

5.2 · 6.1% of the exam · Topic 2 of 4

Technical Fundamentals

The Claude API is HTTP. Official SDKs wrap that REST surface with types, retries, and streaming helpers. Token streaming is server-sent events on that HTTP response. A WebSocket is a different, two-way connection, and it is not how the Messages API delivers tokens.

Learning objectives

  • Describe the Messages API as a stateless HTTPS request that returns JSON or a server-sent event stream.
  • Choose an official SDK as a client of that API, and know what the SDK does not add.
  • Stream a long response with SSE, and accumulate events into one message.
  • Keep a WebSocket, if you need one, between your client and your server.

Detailed theory

What this skill covers

Technical fundamentals are 6.1% of the exam, the largest slice of Domain 5. The skill is how a program reaches Claude: SDKs wrapping a REST API, and WebSockets as a transport you must not confuse with that API.

The model does not care which language built the JSON. It cares that the body is a Messages request. Your job is to know which process holds the connection, the history, and the retries.

REST, and nothing stored for you

A non-streaming Messages call is an HTTPS POST to the Messages endpoint. The body is JSON: model, max_tokens, messages, and the optional system, tools, thinking, and output_config fields. A success is a JSON message. An error is the error object from Domain 4.

The API does not keep your conversation. The next turn is a new HTTP request that includes every message so far, plus the new user content. That is why a long thread costs more input on every turn, and why two servers can continue the same task only if they share the history you stored.

Send the anthropic-version header and the API key. The official client libraries set those. A raw HTTP client has to set them itself. The request id comes back on the response for the failures you will file.

What an SDK wraps

The official Python, TypeScript, and other SDKs are clients for that REST API. A messages.create call becomes the same JSON a curl command would send. You gain typed parameters, typed errors, a default retry on connection failures, rate limits, and 5xx responses, and a place to read the request id.

The SDK does not add a session, a hidden memory, or a different model. Bedrock, Google Cloud, Foundry, and Claude Platform on AWS can be reached through the same style of client, with that platform's credentials and that platform's model id. The message shape stays a message. The id you pin is the one you evaluated on that platform.

Use the SDK in a supported language. Write raw HTTP when you are in a language without a maintained client, or when you are proving exactly which bytes left the process. A raw client that streams has to parse server-sent events itself. The SDK's stream helper accumulates those events into a final message.

const stream = anthropic.messages.stream({
  model: "claude-sonnet-5-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Summarize the incident in five lines." }],
});
const message = await stream.finalMessage();

Streaming is server-sent events

Set stream to true and the success response is server-sent events, not one JSON blob at the end. Each event has a name and a JSON payload: the message starts, content blocks start, text arrives in deltas, blocks stop, and the message stops. An error can arrive after HTTP 200. The events before that error are a partial turn.

People turn streaming on to show tokens as they arrive, and to keep a long generation from looking like a dead HTTP connection. Idle proxies drop quiet sockets. A non-streaming call with a very large max_tokens is the call that times out. The TypeScript SDK refuses a non-streaming request it expects to run on the order of ten minutes. Streaming, or an explicit longer timeout, is how that call proceeds.

The client reads the stream. It does not send further tokens back on the same response. Tool results and the next user turn are a new HTTPS request.

WebSockets are a different socket

A WebSocket is a long-lived connection that both sides can write to, framed as messages, usually opened with an HTTP upgrade. Chat products use them so a browser can send and receive at any time without a new request per keystroke.

The Messages API does not deliver Claude's tokens on a WebSocket. It delivers them as JSON or as server-sent events. A browser that needs a live channel opens that channel to your server. Your server calls the Messages API over HTTPS, reads the SSE stream, and forwards the deltas across the socket you own.

Putting the Anthropic API key in the browser so the page can open either kind of connection publishes the key. The key stays on the server that is allowed to call the API. The socket, if you have one, carries your own session, your own auth, and the text you chose to forward.

Where the history lives

Because each REST call is independent, the store is yours: a database row, a cache, an agent transcript. The SDK object you constructed at process start does not remember previous messages.create calls. You pass the array.

An async SDK client lets your process wait on many HTTP calls at once. It does not merge those calls into one context. Each call has its own model, its own messages, and its own bill. A fan-out of Haiku classifications is many REST requests, not a WebSocket fan-out to the model.

Core concepts

Messages REST call

What
An HTTPS POST whose body is the model, the token cap, and the messages, and whose success is JSON or an event stream.
Why
Every SDK feature bottoms out in this request.
When
You are tracing a call or writing a client the SDK does not cover.
When not
You are choosing Opus versus Haiku. That choice is a field on the request, not a different protocol.

Official SDK

What
A typed client that builds the REST request, retries transient failures, and can accumulate a stream.
Why
It removes hand-rolled header and SSE bugs without changing the model.
When
The language has a maintained Anthropic SDK.
When not
You need a transport the Messages API does not speak. The SDK will not open a WebSocket to the model.

Server-sent events

What
A one-way stream of events on the HTTP response after stream is set true.
Why
This is how token deltas arrive, and how a long generation keeps the connection in use.
When
A person is watching the answer, or the output cap implies a long run.
When not
The client also needs to send tool results on that same response. Results go on the next POST.

WebSocket

What
A persistent two-way connection between two of your processes, or between a browser and your server.
Why
It is the wrong name for Messages streaming, and the right name for a live channel you operate.
When
A client must push and receive without a new HTTP request each time, and your server is the other end.
When not
You want Claude's tokens. Call the API and read SSE.

Client-side transcript

What
The message array your application stores and resends.
Why
The API and the SDK forget the call when the response ends.
When
A conversation continues, or a second server resumes it.
When not
You expected the SDK constructor to remember the previous user. It does not.

Practical examples

The key in the browser

A prototype opens a socket from the page to a vendor endpoint and ships the Anthropic API key in the query string so tokens can stream to the user.

The key belongs on a server you control. The page can use a WebSocket to that server. The server calls the Messages API with the SDK, reads the SSE deltas, and writes them to the socket. The browser never sees the key.

The quiet POST

A batch of long summaries sets max_tokens to 32,000 and leaves stream false. Proxies close the connection before the JSON body arrives. The client reports a timeout and no message.

The generation was too quiet for the path. Stream the response, or set a timeout that matches the job. The final message is the same either way once the events are accumulated.

Claude-specific considerations

  • The Messages API is stateless HTTPS. The next turn is a new POST that includes the history.
  • Official SDKs retry connection errors, 429s, and 5xx responses. They still surface a 400 from a bad body.
  • stream true is server-sent events. The TypeScript SDK's stream helper can return the assembled Message.
  • A non-streaming call that the TypeScript SDK expects to exceed about ten minutes throws unless you stream or raise the timeout.
  • An error event can follow HTTP 200 on a stream. Discard the partial turn.
  • Platform SDKs still send messages. Pin the model id for Bedrock, Google Cloud, Foundry, or the Claude API, matching the platform you tested.
  • The API key stays on the server that is allowed to hold it.

Architecture decisions

SituationChooseBecause
A supported language needs to call Claude.The official SDK.It sends the REST request, types the errors, and retries transient failures.
The answer is long, or a person is reading it live.Streaming SSE, then the assembled message.Deltas keep the connection active and can be shown as they arrive.
A browser must see tokens as they arrive.A connection from the browser to your server, and an SDK call from that server.The API key and the Messages request stay off the page.
A second turn depends on the first answer.Store the transcript and send it on the next POST.The SDK does not keep a server session for you.
You need the bytes for a language without an SDK.HTTPS with the version header, the key, and an SSE parser if you stream.The protocol is still REST. Only the helper library is missing.

Tradeoffs

An SDK hides headers, retries, and event parsing. Raw HTTP shows every byte and leaves those jobs to you. A WebSocket feels live and still has to end at a server that speaks the Messages API.

AxisYour processThe Claude API
ClientSDK method call.HTTPS JSON or SSE.
Live UIWebSocket or SSE to the browser.SSE from the Messages response.
MemoryThe transcript you store.No conversation object between calls.
Long outputA timeout you configured.A stream that keeps sending deltas.

Quick reference

  • Messages is REST. One call is one HTTPS request with the full message array.
  • An official SDK wraps that request. It does not add memory or a different model.
  • stream true uses server-sent events. The client reads deltas and can assemble a Message.
  • Tool results and the next user turn are a new POST. They are not frames on the SSE response.
  • A long non-streaming call can die on a quiet connection. Stream it.
  • A WebSocket is two-way and persistent. It belongs between your client and your server.
  • The Messages API does not stream tokens over a WebSocket.
  • Keep the API key on the server. The browser talks to you.
  • An async client runs many REST calls. It does not merge them into one window.
  • Platform model ids differ. The protocol stays a message.

Decision rules for the exam

If the question says…The answer is likely…
"the SDK will remember the chat"You resend the messages array
"stream the tokens"Server-sent events on the Messages response
"open a WebSocket to the model"Use SSE for tokens. A socket, if any, ends at your server
"the HTTP call sits quiet for minutes"Stream, or raise the timeout
"put the API key in the page"The server holds the key and calls the SDK
"tool result mid-stream"Finish the turn, then POST the tool_result
"same JSON, different cloud id"Pin the id you evaluated on that platform
"async client means one shared context"Each call is its own request and bill

Common exam traps

TrapCorrect answer
Streaming is a WebSocket to Anthropic.Streaming is server-sent events on HTTP.
The SDK stores the conversation on Anthropic's servers.You store it and send it again.
SSE lets the client push tool results upstream.The response is one-way. The next request carries the result.
A raw curl call uses a different model than the SDK.Both send the Messages body. The model field decides.
Streaming is cheaper.The final message is priced the same. Streaming changes delivery.

Open the Domain 5 sheet

Exam tips

  • The largest Domain 5 slice is this transport. Name the protocol before you name the model.
  • If the stem says WebSocket and Claude tokens in the same breath, split them: socket to your server, HTTPS to the API.
  • If the stem says the client forgot the earlier turn, the history was not in the next request.

Common mistakes

  • Opening a WebSocket from the browser to the Claude API with the secret in the page.

    The page talks to your server. The server calls the SDK.

  • Assuming messages.create remembers the previous user content.

    Append the assistant turn and the new user turn to the array you store.

  • Leaving stream false on a 30,000-token generation.

    Stream the call so the connection carries deltas instead of sitting quiet.

  • Parsing only the first SSE delta and dropping the rest.

    Accumulate until message_stop, and discard the turn if an error event arrives.

Practice questions

Original questions for this topic. They are study items, not questions from the live exam.

A TypeScript service uses the official SDK. After a successful call, the next user question is sent as messages containing only that new question. Claude answers as if the earlier instructions never happened. What was missing?

Choose one answer

A product wants Claude's reply to appear word by word in a browser. Where does the Anthropic API key live, and how do the tokens arrive from Claude?

Choose one answer

Summaries set max_tokens to 32000 and leave streaming off. Callers on a corporate proxy time out with an empty body. Which change matches the failure?

Choose one answer

A maintained Python service needs to call Claude on the Claude API. What should build the HTTP request?

Choose one answer

Scenario questions

The live incident page

An incident console must show a running summary as the model writes it. A teammate proposes a WebSocket from the browser directly to Anthropic, with the API key in an environment variable that the front-end build inlines. Tool calls during the summary need to run in your network.

Which design matches the API?

Choose one answer

Build exercise

Trace one turn across the transports

Intermediate · 35 minutes

What you will learn

  • Which hop is REST, which is SSE, and which may be a WebSocket.
  • Where the transcript and the API key live.
  • When the next POST starts.
  1. Step 1

    Draw three boxes

    Label the browser, your server, and the Claude API. Draw the connection you want for a live summary.

    Why: The exam item is which protocol sits on which hop.

    You should see: A line from browser to server, and a line from server to the API.

  2. Step 2

    Name the protocols

    Mark the browser hop as your socket or your own HTTP. Mark the API hop as HTTPS, with SSE when the summary streams.

    Why: WebSocket and SSE get swapped in stems.

    You should see: SSE labeled on the API response only.

  3. Step 3

    Write the two bodies

    Sketch the first messages array, then the array after an assistant tool_use and your tool_result. Both are POSTs.

    Why: The SDK will not invent the second body.

    You should see: Two requests, the second longer by the assistant turn and the tool result.

  4. Step 4

    Note the timeout

    If max_tokens is large, write stream true next to the call and one sentence about a quiet proxy.

    Why: Long generations fail closed when the response stays empty.

    You should see: A streaming flag on the long call.

Review checklist

Checks are saved in this browser.

Key takeaways

  • Official SDKs are clients for the Messages REST API. They type the body, retry transient errors, and can accumulate SSE.
  • You hold the transcript. Each turn is a new POST.
  • Token streaming is server-sent events. A WebSocket, when you need one, ends at your server.
  • A long quiet response is a timeout risk. Stream it.

Sources

  • Streaming messages — Server-sent events, event types, and why long calls should stream.
  • Client SDKs — Official libraries that wrap the REST API.
  • Build with Claude — The Messages request and the application loop around it.
  • CCDV-F blueprint notes — Domain 5 technical fundamentals: SDKs, REST, and WebSockets. Study notes, not exam items.

Domain 5 overview · Quick reference

View progress