Claude API · Tools and structured output
Return a tool result
Run the tool, then send its output back in a user message whose tool_use_id matches. Return all results for parallel calls in one message.
tool_use = next(b for b in response.content if b.type == "tool_use")
result = run_tool(tool_use.name, tool_use.input) # your code
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": [
{"type": "tool_result", "tool_use_id": tool_use.id, "content": result},
]})
Official Claude API documentation
More tools and structured output commands
- Let the SDK run the tool loop
- Define a tool by hand
- Guarantee valid tool input
- Get a validated Python object back
- Constrain output with a JSON Schema
All tools and structured output commands · Claude API cheatsheet