Claude API Function Calling Example in Python
If you're searching for a Claude API function calling example in Python, you're almost certainly trying to get Claude to call a function in your code — a weather lookup, a database query, a calculator — rather than just generate text. Claude doesn't call your function directly; it tells you which function to call and with what arguments, in a structured response. Your Python code runs the function, sends the result back, and Claude uses it to write the final answer.
This article walks through exactly that loop, with working code, so you can go from zero to a functioning tool-use integration in a few minutes.
What "function calling" means in the Claude API
Anthropic calls this feature tool use, not function calling, but it's the same concept you'd know from other LLM APIs. You describe one or more tools (name, description, JSON schema for inputs) in your request. Claude decides whether a tool is needed to answer the user's question. If it is, Claude returns a tool_use content block instead of plain text, containing the tool name and the arguments it wants to call it with.
Your code then:
- Parses the
tool_useblock - Runs the actual Python function with those arguments
- Sends the result back to Claude as a
tool_resultmessage - Receives Claude's final, natural-language response
It's a round trip, not a single call — this is the part most people get wrong when they first implement it.
A minimal working example
Here's a complete example using the official anthropic Python SDK with a single tool: a fake weather lookup function.
import anthropic
import json
client = anthropic.Anthropic(api_key="YOUR_API_KEY")
def get_weather(city: str) -> str:
# In real code, call a weather API here
return json.dumps({"city": city, "temp_c": 18, "condition": "cloudy"})
tools = [
{
"name": "get_weather",
"description": "Get the current weather for a given city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, e.g. 'Berlin'"}
},
"required": ["city"]
}
}
]
messages = [
{"role": "user", "content": "What's the weather like in Lisbon right now?"}
]
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=tools,
messages=messages
)
# Check if Claude wants to use a tool
tool_use_block = next(
(block for block in response.content if block.type == "tool_use"), None
)
if tool_use_block:
tool_name = tool_use_block.name
tool_input = tool_use_block.input
if tool_name == "get_weather":
result = get_weather(**tool_input)
# Send the tool result back to Claude
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use_block.id,
"content": result
}
]
})
final_response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
tools=tools,
messages=messages
)
print(final_response.content[0].text)
else:
print(response.content[0].text)
Run this and Claude will respond with something like "It's currently 18°C and cloudy in Lisbon." — but only after your get_weather function actually executed. That's the core pattern. Everything else (multiple tools, parallel tool calls, forced tool choice) builds on this loop.
Common mistakes
Forgetting to echo the assistant's turn back. When you send the tool_result, you must also include the original assistant message containing the tool_use block in your message history. Skipping it causes a 400 error because Claude expects to see the tool call it made.
Wrong tool_use_id. The tool_result block must reference the exact id from the tool_use block it answers. If you're handling multiple tool calls in one response, match each result to its corresponding id.
Overly vague schemas. Claude relies entirely on your description fields and JSON schema to decide when and how to call a tool. A schema like {"type": "object"} with no field descriptions will produce inconsistent arguments. Be as explicit as you'd be writing documentation for a junior developer.
Not handling the "no tool needed" case. Claude won't always call a tool, even if one is available — if the user asks something unrelated, it just answers directly. Always check response.content for a tool_use block before assuming one exists.
Scaling this beyond a demo
The example above works fine for a single script, but production use adds real complexity: retrying failed tool calls, logging every tool invocation for debugging, handling streaming responses that interleave text and tool-use blocks, and managing API keys across multiple services or team members without hardcoding them.
This is where SubToAPI is useful if you're building on top of Claude access you already have. It exposes your Claude usage as a standard HTTPS API with sub_live_... application keys, so each service or teammate gets its own key instead of sharing one secret. The request shape for tool use is the same JSON structure Claude expects — see /docs/tools for the exact schema — so the Python loop above needs almost no changes:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"tools": [...],
"messages": [...]
}'
You also get usage metadata per key, which matters once you have several functions and several developers calling Claude in parallel and someone needs to know which key burned through the quota. Start with /docs/quickstart if you want to swap your existing key for a SubToAPI one, or check /docs/messages and /docs/streaming for the full request/response reference. Plans start at €9/month on the Solo tier, with team seats on the pricing page and a free trial at signup.
Questions
Does Claude support function calling the same way as OpenAI? Conceptually yes — you define a schema, the model returns a structured call instead of text, and you run the function yourself. Anthropic calls the feature "tool use" and the JSON field names (tool_use, tool_result, input_schema) differ from OpenAI's function_call format, so code isn't directly portable without adapting field names.
Can Claude call multiple functions in one response? Yes. Claude can return multiple tool_use blocks in a single response if the task needs several tools. Your code should loop over all tool_use blocks in response.content, execute each one, and return a matching tool_result for every tool_use_id before continuing.
What happens if my function throws an error? Catch the exception in Python and send the error message back as the tool_result content instead of the function's normal output. Claude will read it like any other result and typically explain the failure to the user or try a different approach, rather than your code crashing the whole exchange.