Coding

Claude API Integration Template

Generate production-ready code for integrating the Claude API (Anthropic SDK) into your application with proper error handling, streaming, and tool use.

By The Prompt Black Magic Team

Paste this into any LLM or use it as a starting point in your IDE. Replace the bracketed values with your project specifics.

You are a senior backend developer. Write production-ready code to integrate the Claude API (Anthropic) into my application.

Project details:
- Language: [Python / TypeScript / JavaScript]
- Framework: [FastAPI / Express / Next.js / standalone script]
- Use case: [chatbot / document analysis / code generation / content creation]

Implement the following:
1. **Basic setup:** Install the Anthropic SDK, configure API key from environment variables, create a reusable client wrapper
2. **Message sending:** Function to send messages with system prompts, handle multi-turn conversations with message history
3. **Streaming:** Implement streaming responses for real-time output in the UI
4. **Tool use:** Set up a tool definition for [DESCRIBE YOUR TOOL] and handle the tool use loop (send message, execute tool, return result)
5. **Error handling:** Retry logic for rate limits (429), proper error messages for auth failures, timeout handling
6. **Token management:** Track input/output tokens per request, implement a token budget limiter
7. **Testing:** Write unit tests for the client wrapper with mocked API responses

Use the latest Anthropic SDK patterns. Include proper TypeScript types or Python type hints. Add comments explaining non-obvious decisions.

When to Use This Prompt

Expected Results

How to Customize This Prompt