โ AI App Building (No-Code & Low-Code)
Day 2 of 14
Building with the Claude API
Getting Started: Models, Pricing, and Choosing the Right One
Anthropic's Claude API offers several model tiers. Claude Opus is the most capable but most expensive โ $15/million input tokens, $75/million output tokens (pricing subject to change, check console.anthropic.com). Claude Sonnet is the sweet spot for most production applications: strong capability at $3/$15 per million tokens. Claude Haiku is fastest and cheapest at $0.25/$1.25 per million tokens โ ideal for high-volume, lower-complexity tasks.
Choose your model by task complexity, latency requirements, and cost sensitivity. For BathCheck's report generation (complex, infrequent, quality-critical): Sonnet or Opus. For real-time chat assistance or simple classification: Haiku. For batch processing of historical records: Haiku with a quality check on a sample using Sonnet. Never default to the most capable model for everything โ it's unnecessary cost.
Token economics: 1,000 tokens โ 750 words. A typical assessment report generation might use 500 input tokens (the data) + 1,000 output tokens (the report) = $0.007 per report at Sonnet pricing. At 10,000 reports/month, that's $70/month โ not $7,000. Run these calculations early to validate your unit economics.
Production Patterns: Streaming, Error Handling, and Retry Logic
Streaming responses (receiving tokens as they're generated) dramatically improves perceived performance for user-facing features. Instead of a 5-second wait for a full response, the user sees text appearing progressively. Implement streaming with the Anthropic SDK using the streaming parameter โ it returns an async iterator of token events. For web apps, stream via Server-Sent Events (SSE) to the frontend.
Error handling for production API calls: Claude API errors fall into categories โ rate limits (429), server errors (5xx), invalid requests (400s), and context length exceeded. Rate limits require exponential backoff with jitter (not fixed retry intervals, which cause thundering herd problems). Context length errors require input truncation logic โ decide in advance which parts of the context are expendable.
Prompt caching is a cost optimisation for repeated system prompts: Claude API's cache_control parameter allows caching of prefixes that are reused across requests. If your system prompt is 2,000 tokens and you make 10,000 requests/month, that's 20M tokens of system prompt cost. With caching, you pay full price for the first request and 90% less for cache hits โ on a 1-hour cache window, this can reduce costs by 70โ80% for high-traffic applications.
โก Today's Action
Make your first production-quality Claude API call. Build a small script that takes a piece of text input relevant to one of your apps (an assessment form submission, a user query, a product description) and returns a structured JSON response. Focus on getting the system prompt right and handling errors correctly.
๐ก Pro Tip
Use the Anthropic Workbench (console.anthropic.com) for iterative prompt development before writing any production code. You can test prompts, compare models side by side, and see token counts in real time โ this saves hours of code-test-code cycles.