Claude SDK & Anthropic APIs
This domain covers 22% of the AI-ADV exam and tests your ability to build production-grade applications using Anthropic's Python and TypeScript SDKs, advanced API features, and integration patterns. Questions probe exact parameter names, default values, behavioral limits, and architectural decisions — not just conceptual understanding. Expect scenario-based questions that require you to distinguish between similar features and identify correct implementation patterns.
Anthropic Python/TypeScript SDK: Client Setup, Messages.create, Streaming
Client Setup
The Anthropic SDK is available for Python (anthropic) and TypeScript/Node.js (@anthropic-ai/sdk). Authentication uses the ANTHROPIC_API_KEY environment variable by default, or you pass it explicitly to the client constructor.
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-...") # or reads from env
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
messages.create Parameters
| Parameter | Type | Required | Notes |
|---|---|---|---|
model | string | Yes | e.g. claude-opus-4-5, claude-sonnet-4-5 |
max_tokens | integer | Yes | Upper bound on output tokens |
messages | array | Yes | List of {role, content} objects |
system | string or array | No | System prompt; array form enables cache_control |
temperature | float | No | Default 1.0; range 0.0–1.0 |
stream | boolean | No | Default false |
tools | array | No | Tool definitions for function calling |
tool_choice | object | No | Controls tool selection behavior |
Streaming
Streaming returns server-sent events. Use the .stream() helper for managed streaming or pass stream=True to messages.create.
with client.messages.stream(model="claude-opus-4-5", max_tokens=1024, messages=[...]) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
message = stream.get_final_message()
Common traps:
- Forgetting
max_tokensis required — omitting it raises a validation error. - Assuming
temperaturedefault is 0 — it is 1.0. - Confusing
.stream()helper (managed, hasget_final_message()) with rawmessages.create(stream=True)(returns an iterator of raw events). stop_reasonvalues:end_turn,max_tokens,stop_sequence,tool_use— not "finish" or "stop".
Tool Use (Function Calling): Tool Definitions, input_schema, tool_result Blocks
Tool Definition Structure
Each tool is a JSON object with three required fields:
{
"name": "get_weather",
"description": "Get current weather for a city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
Tool Use Flow
Bold caption: Tool use request-response cycle showing message roles and content block types.
Content Block Types
| Block Type | Role | Purpose |
|---|---|---|
text | assistant | Regular text output |
tool_use | assistant | Model requests a tool call; has id, name, input |
tool_result | user | Application returns result; has tool_use_id, content |
thinking | assistant | Extended thinking output (read-only) |
tool_choice Options
| Value | Behavior |
|---|---|
{"type": "auto"} | Model decides whether to use tools (default) |
{"type": "any"} | Model must use at least one tool |
{"type": "tool", "name": "X"} | Model must use the specific tool X |
Common traps:
tool_resultblocks go in a user role message, not assistant.- The
tool_use_idintool_resultmust exactly match theidfrom thetool_useblock. input_schemamust be valid JSON Schema — missing"type": "object"at root is a common error.- Setting
tool_choiceto{"type": "any"}does not guarantee a specific tool — use"tool"type for that. - Tool results with errors: set
"is_error": truein thetool_resultblock rather than omitting it.
Worked scenario: You need Claude to always call your search_database tool before answering. Use tool_choice: {"type": "tool", "name": "search_database"}. Using "any" would allow Claude to call any tool in your list.
Unlock with a paid plan
11 more sections (about 2,125 words) continue below. Every paid plan unlocks all AI Advanced study guides, the full question bank and mock exams, from $4.97/month.