Claude API
Build with the Claude API - messages, system prompts, tool use, streaming, prompt caching, and best practices for production AI applications.
skill: Claude API category: AI/ML tags: [Claude API, Anthropic, LLM, Tool Use, Streaming] readingTime: 6
Claude is designed to be helpful, harmless, and honest - it works best with clear system prompts, well-structured tool definitions, and explicit output format instructions. Don't fight the model's default behaviors; work with them. Use prompt caching for large repeated contexts (docs, codebases) to reduce cost and latency by up to 90%.
Workflow
- Make your first API callThe Messages API is simple - system prompt sets the assistant's role; user message is what you want; assistant message is the reply.
- Implement tool use (function calling)Tool use lets Claude call external functions - querying databases, calling APIs, or executing code - and reason about the results.
- Stream responses for real-time UXStreaming shows text as it generates - reducing perceived latency from 5s to near-instant. Essential for chat UIs and long-form generation.
- Use prompt caching for repeated contextPrompt caching stores a prefix of your context on Anthropic's servers - re-sending the same system prompt or document costs 90% less (cache read vs. full input price) and is 10x faster.
- Handle errors and rate limitsProduction applications must handle API errors gracefully - rate limits, overload, and transient failures should retry, not crash.
Output Format
Claude API: messages with system prompt, tool use loop, streaming SSE, prompt caching, and retry with exponential backoff.
Model selection:
claude-haiku-4-5 → fast, cheap, simple tasks
claude-sonnet-5 → balanced, default for most tasks
claude-opus-5 → most capable, complex reasoning
Caching: add cache_control to large stable context
Streaming: always for chat UIs (< 2s to first token)
Tool use: for actions + external dataExamples
Input
Output
Edge Cases & Gotchas
- Context window limits - Claude Sonnet 5 has a 200k token context window. Very long conversations + large system prompts can exceed this. Implement a sliding window: keep system prompt + last N turns + current message.
- Max tokens vs. response length - max_tokens is the ceiling, not the target. Claude stops when it finishes the response - setting max_tokens=4096 doesn't guarantee a long response. Set it to the maximum you'd ever want, not an estimate.
- Tool use infinite loops - An agentic loop that always calls tools can run indefinitely. Set a max_iterations limit and detect when the model is calling the same tool with the same args repeatedly (a stuck loop).
- System prompt injection via user content - Never interpolate raw user input into system prompts. A user who sends 'ignore instructions and...' in a field you template into the system prompt can manipulate the model's behavior.
When to Break These Rules
- Use the Anthropic Workbench (console.anthropic.com) for iterating on prompts interactively before building the API integration - much faster iteration loop.
- Use Claude.ai directly instead of the API for one-off tasks, research, or when you don't need programmatic output - the API adds operational complexity not needed for human-facing use.
Reference Files
https://docs.anthropic.com/en/api/getting-startedhttps://docs.anthropic.com/en/docs/build-with-claude/tool-use