Topic 5 of 5 · 5 min read
First-call mistakes that leak keys or truncate answers
Goal: Find and fix the mistakes in a first API integration that would leak a key, silently truncate output or break when a model alias changes.
Most first integrations fail in the same five ways, so review yours against a short checklist before it leaves your laptop.
Check yourself · 1 of 3
Which of these errors is worth retrying after a short pause?
Why it matters
A first call that "works" proves little. It was tested with one short prompt, one good network and your own key. Production adds long inputs, slow responses, rate limits and other people reading your repo. The mistakes below stay hidden until then, and each one has a cheap fix at the start and an expensive one later.
The checklist
- No key in the code. Read it from the environment, keep
.envout of git, and rotate any key that was ever exposed. - An explicit output limit. Set
max_tokens(or the provider's equivalent) to fit the real answer. Too low truncates; very high lets a runaway reply cost money. - Check why it stopped. Treat
max_tokensorMAX_TOKENSas an error or a retry case, not as a finished answer. - Pinned model id. Keep it in one config value, not scattered through the code, and use a snapshot id in production.
- Handle errors. Rate limits (HTTP 429), overloaded servers and network timeouts happen. Set a timeout, retry with backoff on retryable errors, and log the request id. The official SDKs retry some failures for you, but you still decide what the user sees when they give up.
Before and after
-client = Anthropic(api_key="sk-ant-...redacted")
-r = client.messages.create(model="latest-model-alias", messages=msgs)
-print(r.content[0].text)
+client = Anthropic(timeout=30.0)
+r = client.messages.create(model=settings.MODEL_ID, max_tokens=500, messages=msgs)
+if r.stop_reason == "max_tokens":
+ log.warning("truncated req=%s", r._request_id)
+print(r.content[0].text)
The old version leaks a key, has no output limit (and the Claude API would reject it), trusts an alias, and cannot tell a cut-off answer from a complete one. The new one fixes each point in four lines.
What goes wrong
- A key in a public repo. It is found, used and billed, and you learn about it from the invoice.
- Silent truncation. Users receive half a summary or broken JSON, and nobody sees an error.
- Alias drift. A vendor update changes output format and a parser breaks on a Monday.
- No error handling. One 429 or timeout becomes an unhandled exception and a blank page.
- Retrying everything. A loop that retries a 400 error, which will never succeed, burns time and money. Retry only transient errors.
Examples
Which errors to retry
400 invalid_request fix the request, do not retry
401 authentication fix the key, do not retry
429 rate limited retry after a pause
500/529 server busy retry with backoff
Codes follow the provider's documented error list. Retrying a 400 repeats the same bad request and fails again.
A review comment on a first integration
"Line 3 hard-codes the key. Line 7 has no max_tokens. Line 9 prints the text without checking stop_reason. The model id is a floating alias, so move a pinned id into settings and note when to re-test it."
Key points
- Never hard-code a key; read it from the environment and rotate any key that was exposed.
- Always set an output limit that fits the real answer, and check the stop reason.
- Pin a snapshot model id in one config value instead of using a moving alias in production.
- Set a timeout and retry only transient errors such as 429 and 5xx, with backoff.
- Log the request id and token usage so failures and costs can be traced.
Common mistakes
- Leaving a test key in the code and forgetting about it until the repo goes public.
- Treating any non-error response as a complete answer.
- Spreading the model id as a string literal through ten files.
- Wrapping the call in a bare try/except that swallows every error, including a wrong key.
Check yourself · 2 of 3
Your summaries sometimes end mid-sentence in production. Which check finds the cause fastest?
Check yourself · 3 of 3
A model alias is used in production and your output format changes after a vendor update. What prevents this next time?
Practice items, not counted towards your mastery.
Start here
- Claude API overview (opens in a new tab)DocsFree
Authentication headers, the Messages endpoint and the official SDK list in one place.
- Working with messages (Claude docs) (opens in a new tab)DocsFree
Roles, multi-turn requests, response fields and stop reasons with runnable examples.
- OpenAI developer quickstart (opens in a new tab)DocsFree
The same first call in the Responses API, with its SDK setup and key handling.
More for this topic
- Errors (Claude API) (opens in a new tab)DocsFree
HTTP error codes, the error body shape and the request id to log for support.
- About secret scanning (GitHub Docs) (opens in a new tab)DocsFree
How leaked keys get detected in repositories and what to do when one is found.