AI Engineering Tutorial · LLM Basics · Lesson 4 of 25
Calling an LLM API
What a real API call looks like, the parts of the response, and how to keep keys safe.
A real call
With an SDK, a call is a few lines. This is what it looks like with the Anthropic Python SDK
(pip install anthropic, key in the ANTHROPIC_API_KEY environment variable):
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=300,
system="You are a concise assistant.",
messages=[{"role": "user", "content": "Explain HTTP caching in two sentences."}],
)
print(response.content[0].text)
print(response.usage.input_tokens, response.usage.output_tokens, response.stop_reason)
The response
- content: a list of blocks; plain answers have one
textblock. - usage: input and output tokens, for cost tracking.
- stop_reason: why generation stopped:
end_turn(finished),max_tokens(cut off),tool_use(wants a tool), and others.
responses = [
{"text": "HTTP caching stores responses...", "stop_reason": "end_turn", "usage": (42, 38)},
{"text": "HTTP caching stores responses so that repeated requests can be served without", "stop_reason": "max_tokens", "usage": (42, 20)},
]
for r in responses:
note = "complete" if r["stop_reason"] == "end_turn" else "CUT OFF: raise max_tokens or ask for less"
print(f"{r['stop_reason']:10} in={r['usage'][0]} out={r['usage'][1]} {note}")
Output
end_turn in=42 out=38 complete max_tokens in=42 out=20 CUT OFF: raise max_tokens or ask for less
Keep keys safe
API keys belong in environment variables or a secrets manager on the server. Never put them in browser or mobile code, and never commit them. See the API overview (Anthropic's docs; other providers work the same way).
Test yourself
A response's stop_reason is max_tokens. What happened?
The answer was cut off because it reached the max_tokens limitThe model refusedThe model wants to call a toolThe answer is complete
Show answer
Answer: The answer was cut off because it reached the max_tokens limit
max_tokens means generation stopped at your limit; the text may be incomplete.