Skip to main content

Production API Snippets

Intermediate

Battle-tested shapes for building on the API. Adapt and harden for your stack.

A resilient call wrapper (retries + backoff)

import time, random, anthropic
client = anthropic.Anthropic()

def ask(messages, model="claude-sonnet-4-6", max_tokens=1024, system=None):
for attempt in range(5):
try:
return client.messages.create(
model=model, max_tokens=max_tokens,
system=system or anthropic.NOT_GIVEN, messages=messages,
)
except (anthropic.RateLimitError, anthropic.APIStatusError) as e:
if attempt == 4:
raise
time.sleep(min(2 ** attempt + random.random(), 30))

(SDKs also retry transient errors by default — know your client's behavior before stacking your own. See Errors & Rate Limits.)

Streaming chat

with client.messages.stream(model="claude-sonnet-4-6", max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}]) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)

Tool-use loop (skeleton)

messages = [{"role": "user", "content": "What's the weather in Rome?"}]
while True:
resp = client.messages.create(model="claude-sonnet-4-6", max_tokens=1024,
tools=TOOLS, messages=messages)
if resp.stop_reason != "tool_use":
break
messages.append({"role": "assistant", "content": resp.content})
results = [run_tool(b) for b in resp.content if b.type == "tool_use"]
messages.append({"role": "user", "content": results}) # tool_result blocks

:::warning Hardening Cap iterations and cost, validate tool inputs, and keep secrets in env vars. For autonomous use, read Securing Agents. :::

Next