CCDV-F
CCDV-F quick reference
The facts worth having in your head when you sit down. Print it, or read it on the way there.
Current model IDs, prices and limits change; read them from docs.anthropic.com. This sheet covers the structure and rules that stay stable.
1. Agents and Workflows
| Concept | Remember |
|---|---|
| Agent loop | call API, read stop_reason, run tools, append assistant content + tool_result user message, repeat |
end_turn / tool_use / max_tokens | finished / run tools and call again / truncated, never execute a half-formed tool call |
pause_turn / refusal | resend conversation unchanged / stop and log |
| Workflow vs agent | code decides steps vs model decides steps; start with the simplest |
| Patterns | chaining (sequential), routing (classify then dispatch), parallelisation (sectioning or voting), orchestrator-workers (runtime decomposition), evaluator-optimiser (generate, grade, loop) |
| Three build options | manual loop (own everything), SDK tool runner (loop automated, your tools only), Claude Agent SDK (Claude Code harness with built-in tools) |
| Agent SDK options | allowed_tools, permission_mode (default, acceptEdits, plan, bypassPermissions), system_prompt, mcp_servers, cwd, max_turns, hooks, resume |
2. Applications and Integration
- Required request fields:
model,max_tokens,messages.systemis top-level, not a role. - Roles: first message is
user; consecutive same-role messages merge; the API is stateless so resend full history. - Response:
contentarray of typed blocks (text,tool_use,thinking),stop_reason,usage. - Raw HTTP headers:
x-api-key,anthropic-version,content-type; betas viaanthropic-beta. - SDK client: zero-arg constructor reads
ANTHROPIC_API_KEY;max_retriesdefault 2;timeoutseconds (Python) vs milliseconds (TypeScript). - Typed errors:
AuthenticationError401,RateLimitError429,APIStatusErrorother; branch on class, not message text. - Images:
imageblock, sourcebase64|url|file; JPEG, PNG, GIF, WebP; image before text. - PDFs:
documentblock,application/pdf; text plus page images, so more tokens than text; optionalcitations: {enabled: true}(all docs or none; not with structured output). - Files API:
client.files.uploadonce, reference byfile_id; storage ops free, content billed as input; uploads not downloadable. - Streaming events in order:
message_start→content_block_start→content_block_delta* →content_block_stop→message_delta(stop_reason, output usage) →message_stop;pingkeep-alives;errorcan arrive mid-stream. - Delta types:
text_delta,input_json_delta(partial_json, concatenate then parse),thinking_delta. - Helpers:
client.messages.stream()→get_final_message()/finalMessage(); largemax_tokensrequires streaming. - Batches:
messages.batches.create(requests=[{custom_id, params}]); pollprocessing_statusuntilended; results in any order keyed bycustom_id; outcomessucceeded/errored/canceled/expired; 24 h window, results kept 29 days; discounted; everything except streaming supported. - Chat apps: key only on backend; store full assistant
content; stablesystemfor caching; trim history in whole pairs; persist afterfinalMessage(); opaquemetadata.user_id.
3. Claude Code
| Item | Key fact |
|---|---|
CLAUDE.md | auto-loaded memory; user → project (shared) → CLAUDE.local.md → subdirectory; /init creates it |
| Settings | ~/.claude/settings.json, .claude/settings.json, .claude/settings.local.json; managed policy overrides |
| Permissions | allow/deny rules like Bash(npm test), Edit(src/**); modes default, acceptEdits, plan, bypassPermissions |
| Commands | built-ins /init /clear /compact /model /permissions /mcp; custom in .claude/commands/*.md with $ARGUMENTS |
| Hooks | PreToolUse, PostToolUse, UserPromptSubmit, Stop; exit 2 blocks |
| MCP | `claude mcp add --transport stdio |
| Headless | `claude -p "..." --output-format json |
4. Eval, Testing and Debugging
- Build evals from real inputs; version them with the prompt; grow from production failures.
- Graders: code-based (preferred when checkable) → model-based with rubric and spot checks → human for high stakes.
- Mock the client in unit tests, including
tool_useandmax_tokensresponses; separate small integration suite. - Log
id/request-id,usage,stop_reason, prompt version and model on every call. - Symptoms: cut-off text =
max_tokens; cache never hits = volatile prefix; 404 = model ID; role-alternation 400 = history bug;tool_use_id400 = results not adjacent.
5. Model Selection and Optimisation
| Topic | Rule |
|---|---|
| Tiers | Opus (most capable) > Sonnet (balanced) > Haiku (fastest, cheapest); verify with evals |
| IDs | alias → latest snapshot; dated snapshot is fixed; pin in production; client.models.list() shows access |
| Pricing | per million tokens; output ≫ input; tier-dependent; cache write premium, cache read discount; batch discount; thinking billed as output |
usage sum | prompt size = input_tokens + cache_creation_input_tokens + cache_read_input_tokens |
count_tokens | free, model-specific, includes system + tools + messages; never use third-party tokenisers |
| Caching | prefix match over tools → system → messages; cache_control: {type: "ephemeral"}; ≤ 4 breakpoints; 5 min default, ttl: "1h" optional; refreshed on read; minimum prefix length per model; top-level cache_control = automatic placement |
| Cache proof | cache_read_input_tokens > 0 on repeat; zero means timestamp, ID, unsorted JSON, varying tools or model switch |
| Rate limits | RPM, ITPM, OTPM per org and model; rise with tier; OTPM estimated from max_tokens; cache reads mostly exempt from ITPM; headers retry-after, x-ratelimit-* |
| Errors | 400 fix request · 401 key · 403 permission · 404 model/path · 413 too large · 429 wait retry-after · 500/529 backoff |
| Latency | TTFT grows with input, generation with output; stream, cache, trim, smaller model, lower effort, parallelise |
6. Prompt and Context Engineering
- Instructions and persona in
system; task and data in user message; data wrapped in XML tags like<document>. - Be specific, give reasons, say what to do instead of only what not to do; 2 to 5 diverse
<example>s fix format. stop_sequencesend generation;temperaturewhere supported; assistant prefill is rejected on current models.- Structured output:
output_config: {format: {type: "json_schema", schema}}(replaces deprecatedoutput_format);messages.parse()with Pydantic / Zod →parsed_output. - Schema rules:
additionalProperties: false, every property inrequired, subset of JSON Schema; validate ranges in code; checkstop_reasonbefore parsing. strict: trueon a tool guarantees validtool_use.input.- Context: measure with
count_tokens; truncate whole pairs; summarise old turns; limit tool result size at source; server-side context editing (clears) and compaction (summarises, echo the block back); edits invalidate cache from that point.
7. Security and Safety
| Area | Rule |
|---|---|
| Keys | server-side only; env var or secret manager; never in client code, git, logs or URLs; workspaces per environment; spend limits; rotate create → deploy → verify → disable |
| Admin keys | sk-ant-admin... for Admin API only; cannot call Messages |
| Injection | direct (user) vs indirect (documents, web, tool and MCP results); defences: tagged untrusted data, system prompt stays trusted, least-privilege tools, human approval via your UI, validate inputs and outputs, sandbox, iteration caps, logging |
| Moderation | fast model + structured verdict on input/output/retrieved content; deterministic checks first; stop_reason: "refusal" with stop_details is terminal, do not loop rephrasings |
| Usage Policy | universal prohibitions; high-risk domains need qualified human oversight and disclosure; consumer products disclose AI and protect minors; operator enforces on users; no circumventing safeguards |
| Data | API data not used for training under commercial terms; retention windows documented; zero data retention available with feature caveats |
8. Tools and MCPs
- Definition:
name(^[a-zA-Z0-9_-]{1,64}$),description(what, when, when not, returns),input_schema(object, described properties,required); rendered beforesystemin the cache prefix. tool_choice:auto(default),any,tool+name,none;disable_parallel_tool_use: true; forced forms rejected by some newest models (useauto+ prompt or structured output).- Call:
stop_reason: "tool_use", block{type: "tool_use", id, name, input}; readinputas an object. - Result: user message of
{type: "tool_result", tool_use_id, content, is_error?}; all results for a turn in one message immediately after the assistant turn; content may include text and image blocks. - Never execute a tool call from a
max_tokensresponse; return failures withis_error: true. - Streaming tools:
content_block_start(id, name) →input_json_deltafragments →content_block_stop→ parse. - SDK helpers:
@beta_tool(Python),betaZodTool(TypeScript); tool runner loops for you but you still check stop reasons. - Anthropic-defined: versioned
type(bash, text editor) run client-side; server tools (web search, web fetch, code execution) run on Anthropic's side. - MCP vocabulary: host, client, server; tools, resources, prompts; transports
stdio(local) and streamable HTTP (remote). - MCP connector:
client.beta.messages.create(betas=[...], mcp_servers=[{type: "url", url, name, authorization_token?}], tools=[{type: "mcp_toolset", mcp_server_name}]); both halves required; remote only. - Own client: list tools → map to Messages tools → dispatch
tool_use→callTool→tool_result; SDKanthropic[mcp]helpersmcp_tool/async_mcp_tool. - Agent SDK / Claude Code:
mcp_serversoption orclaude mcp add; toolsmcp__<server>__<tool>must be allow-listed; MCP results are untrusted; never hand a server your Anthropic key.
