Function Calling & Structured Outputs
Give the model tools and typed outputs — the two primitives that turn text prediction into software you can build on.
Define a tool the model can call
Function calling works by describing tools in a JSON schema; the model responds with 'call this tool with these arguments' instead of prose when appropriate. Your code executes the call and returns the result — the model never runs anything itself. Define one real tool:
{ "name": "get_order_status", "description": "Look up the current status of a customer order by its id.", "input_schema": { "type": "object", "properties": { "order_id": { "type": "string", "description": "e.g. ORD-12345" } }, "required": ["order_id"] } }VerifyThe description says WHEN to use the tool, not just what it is — that's what the model actually keys on.Run the tool-use loop
The loop is: send messages + tools → model returns a tool_use block → you execute and append a tool_result → model continues. In Python with the Claude API:
import anthropic client = anthropic.Anthropic() msg = client.messages.create( model="claude-sonnet-5", max_tokens=1024, tools=[get_order_status_tool], messages=[{"role": "user", "content": "Where is order ORD-12345?"}], ) # msg.stop_reason == "tool_use" → execute, append result, call againVerifystop_reason is 'tool_use' and the block contains {order_id: 'ORD-12345'} — the model extracted the argument itself.Force structured output when you need data, not prose
For extraction and classification, constrain the output to a schema instead of parsing prose with regex. Every major provider supports this (structured outputs / response schemas); it turns 'usually valid JSON' into 'valid JSON'. The pattern: define the schema, request strict adherence, parse with a real validator (Pydantic/Zod) anyway.
class Invoice(BaseModel): vendor: str total_cents: int due_date: str # ISO 8601 line_items: list[LineItem] # pass Invoice's JSON schema as the required output format; # validate the response with Invoice.model_validate_json(...)VerifyMalformed model output raises a validation error in YOUR code — failures are loud and typed, not silent string drift.Know the failure modes
Three to design for: the model calls a tool with hallucinated arguments (validate before executing); it answers in prose when you needed the tool (check stop_reason, retry with a nudge); it loops calling the same tool (cap iterations). Tool use is reliable in 2026, but reliable means 99%, and production means handling the 1%.
VerifyYou can name where in your loop each of the three failure modes gets caught.