Guides / Prompting and shipping
How to Integrate the Claude API into Your App
Add Anthropic's Claude API to your app: API keys, the Messages API, system prompts, streaming, errors like 429 and 529, spend limits and safe server-side calls.
Mythex Team · · 6 min read
To integrate the Claude API, create an API key in Anthropic's Claude Console, store it as a server-side secret (usually ANTHROPIC_API_KEY), and call the Messages API from a route on your own server using one of Anthropic's official SDKs. Each request names a model, sets max_tokens (the longest reply you'll accept) and sends the conversation as messages, with your instructions in a separate system prompt. Set a spend limit, stream long replies, and handle rate-limit (429) and overload (529) errors with retries.
This guide covers setup, a safe request flow, prompts for an AI builder, and the errors to plan for. Anthropic details are from its documentation as of September 2026. For how tokens and pricing work in general, see how to use LLM APIs.
What you can build with it
- Chat assistants and help widgets (how to build an AI chatbot app)
- Summaries, rewrites and drafted replies
- Extracting fields from documents, emails and forms
- Answering questions from your own content
- Agents that call your app's functions through tool use
For more ideas, see how to add AI features to your app.
The Messages API in brief
According to Anthropic's API reference, a request is a POST to https://api.anthropic.com/v1/messages with:
| Part | What it is |
|---|---|
x-api-key header | Your API key |
anthropic-version header | The API version, for example 2023-06-01 |
model (required) | Which Claude model to use |
max_tokens (required) | The most tokens the reply may contain; the model may stop earlier |
messages (required) | The conversation: alternating user and assistant turns |
system (optional) | Instructions that apply to the whole conversation |
stream (optional) | Set to true to receive the reply as it's written, via server-sent events |
The API is stateless: to continue a conversation, send the earlier turns again with each request. That's why long chats cost more per message.
Anthropic publishes official SDKs for Python, TypeScript, C#, Go, Java, PHP and Ruby. They set the headers, parse responses, and support streaming. Use one instead of raw HTTP unless you have a reason not to.
Choosing a model
Anthropic offers several Claude models that trade capability against speed and price, and the line-up changes. Pick the smallest model that does your task well, test it on real examples, and keep the model ID in configuration so you can switch without a code change. Check Anthropic's models and pricing pages for current options and rates.
Setting up your account
- Sign in to the Claude Console at platform.claude.com and add billing.
- Create an API key. Keep one key per environment (testing and production) so you can revoke one without breaking the other.
- Set a spend limit. Anthropic's errors page notes that when usage reaches a spend limit you've set, the API returns a 400 error, so your app should show a clear message when that happens.
- Review rate limits for your account in the Console; they're measured per organization.
The request flow
- The user acts in your app: sends a message, clicks "Summarise".
- The browser calls your server route, never Anthropic directly.
- Your server checks the user is logged in and allowed to use the feature, and applies a per-user limit.
- It builds the request: a system prompt with your rules, the user's input, and any data the task needs from your database.
- It calls the Messages API with the key from the environment and a sensible
max_tokens. - It streams the reply to the browser and logs token usage from the response.
Options and trade-offs
| Decision | Simple choice | When to go further |
|---|---|---|
| Streaming | Wait for the full reply | Stream for chat and anything long; Anthropic recommends streaming or batches for long-running requests |
| Output format | Plain text | Structured JSON output when your code needs to read fields |
| Your data | Include the relevant record | Retrieval over your content when there's too much (what RAG is) |
| Actions | Model only writes text | Tool use, where the model asks your code to run a function you define |
| Bulk, non-urgent work | Loop over items | The Message Batches API, which you submit and poll for results |
Prompts to give your AI builder
A first feature:
Add an "Ask about this document" panel. It calls a server route POST /api/ask that checks the user is logged in and owns the document, then calls the Claude Messages API with the official Anthropic SDK and the secret ANTHROPIC_API_KEY. Read the model ID from the secret CLAUDE_MODEL. System prompt: "Answer only from the document provided. If the answer isn't in it, say so." Send the document text and the question, set max_tokens to 800, stream the answer to the page, and never expose the key to the browser.
Guardrails and errors:
Limit each user to 20 questions per hour, tracked in the database. Log input and output tokens from each response with the user ID and the request ID. On 429 or 529 errors, retry with exponential backoff up to 3 times, respecting any retry-after header, then show "The assistant is busy, try again shortly." If the API reports a spend limit error, show "AI features are paused" and alert the admin.
Errors to plan for
From Anthropic's errors documentation, the ones your app will actually meet:
| Code | Type | What to do |
|---|---|---|
| 400 | invalid_request_error | Fix the request; also returned when you hit a spend limit you set |
| 401 | authentication_error | Check the key is present, correct and not revoked |
| 413 | request_too_large | Send less; the Messages API accepts up to 32 MB per request |
| 429 | rate_limit_error | Back off and retry, respecting retry-after |
| 500 | api_error | Retry with backoff; contact support with the request ID if it persists |
| 529 | overloaded_error | Temporary high traffic across all users; back off and retry |
Anthropic's docs say the official SDKs automatically retry transient failures (connection errors, rate limits and 5xx errors) twice by default with exponential backoff, and you can configure that. Every response carries a request-id header; log it so support can trace problems.
Common mistakes
- Calling the API from the browser. Your key becomes public. Always go through your server. See how to keep API keys safe.
- Forgetting
max_tokensis a cap, not a target. Set it to what the feature needs; too low truncates answers, too high risks long, costly replies. - Putting rules in the user message. Stable instructions belong in the
systemprompt; permissions belong in your code. - Sending the whole chat forever. Trim or summarise old turns as conversations grow.
- No spend limit or per-user cap.
- Hard-coding a model ID. Models get updated and retired.
- Ignoring the stop reason. If the reply stopped because it hit
max_tokens, tell the user or raise the cap.
Checklist
- API key created in the Claude Console and stored as a server-side secret
- Separate keys for testing and production
- Spend limit set; clear message when it's reached
- All requests go through your server with permission checks
- System prompt holds the rules; model ID is configurable
-
max_tokensset per feature; long replies streamed - Per-user rate limit in your app
- 429 and 529 handled with backoff; request IDs logged
- Friendly error state that doesn't break the page
- Tested with empty, very long and hostile inputs
Using the Claude API in Mythex
In apps you build on Mythex, Claude features use your own Anthropic key and are billed to your Anthropic account, not to Mythex build credits. That's separate from the Mythex agent that writes your code. Add ANTHROPIC_API_KEY as a project secret through /secrets, project settings or the secrets card in chat, then describe the feature using prompts like the ones above. Publish again after changing secrets on a live app. See Add AI features with your own API keys for a starter prompt. If you're choosing between providers, how to integrate the OpenAI API covers the same ground for OpenAI.
Questions
How do I get a Claude API key?
Sign in to the Claude Console at platform.claude.com, set up billing, and create an API key. Store it as a server-side secret, conventionally named ANTHROPIC_API_KEY, and never put it in browser code.
What does a request to the Claude API need?
As of September 2026, Anthropic's Messages API reference lists three required body fields: model, max_tokens and messages. Direct HTTP calls also need the x-api-key and anthropic-version headers; the official SDKs set these for you.
What is a 529 error from the Claude API?
Anthropic's errors page describes 529 as overloaded_error: the API is temporarily overloaded because of high traffic across all users. Retry with exponential backoff. The official SDKs retry some transient errors automatically.
Can I use Claude with code written for the OpenAI SDK?
Anthropic documents an OpenAI SDK compatibility layer for using Claude through the OpenAI SDK surface. For new code, its own SDKs give access to Claude-specific features, so they are usually the better starting point.