AI Engineering
Structured Output & Tool Use
Techniques for getting Claude to return predictable, machine-parseable output — from JSON extraction to multi-tool orchestration. Tool use (function calling) is the foundation of every agentic workflow, including the Claude Code framework you use daily.
2
Minutes
5
Concepts
+45
XP
1
Structured Output Approaches
Prefilling the Assistant Response

Start the assistant's reply to force a specific format:

python
response = client.messages.create(
    model="claude-sonnet-4-6-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Extract name and age from: 'Matt is 30'"},
        {"role": "assistant", "content": "{"}  # forces JSON output
    ]
)

Simple but fragile — no schema validation. Best for quick extractions where you control the pipeline.

Tool Use for Structured Extraction

Define a tool with a JSON Schema, then force Claude to call it. The output is guaranteed to match the schema.

python
tools = [{
    "name": "extract_person",
    "description": "Extract structured person data from text",
    "input_schema": {
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
            "occupation": {"type": "string", "description": "Job title if mentioned"}
        },
        "required": ["name"]
    }
}]

response = client.messages.create(
    model="claude-sonnet-4-6-20250514",
    max_tokens=1024,
    tools=tools,
    tool_choice={"type": "tool", "name": "extract_person"},  # forced
    messages=[{"role": "user", "content": "Matt is a 30-year-old engineer"}]
)