Skip to content
mentoor

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?

Choose one answer

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

  1. No key in the code. Read it from the environment, keep .env out of git, and rotate any key that was ever exposed.
  2. 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.
  3. Check why it stopped. Treat max_tokens or MAX_TOKENS as an error or a retry case, not as a finished answer.
  4. Pinned model id. Keep it in one config value, not scattered through the code, and use a snapshot id in production.
  5. 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?

Choose one answer

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?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic