LLM Tool Functions
Notes on LLM tool functions from Anthropic's course on Claude. Covers tool schemas and multi-turn conversations with tools.
LLM Tool Functions
Python functions which Claude can call to give it access to realtime info or to perform actions.
Tool Functions
Python function that gets executed. You pass a reference to the tools (the JSON schema of the tool) as part of the client request in order to enable tool usage.
Best Practices for Tool Functions
- Use descriptive names
- Validate inputs
- Provide meaningful error messages
Tool Schema
A JSON Schema that tells Claude what arguments functions take and how to use it.
The input_schema is the actual JSON schema that describes the args of the function.
Generating Schemas with Claude/LLMs
You can use an LLM to generate the schema for your tool function.
e.g.
"Write a valid JSON schema spec for the purposes of tool calling for this function. Follow the best practices listed in the attached documentation."
Message Blocks
LLMs can provide multi block messages with both text and tool usage info.
The text block has info about what the model is doing, the tool use block has instructions for which tool to call and the params to use - the LLM has generated info about how to use the tool given the JSON schema and the message you sent it.
Remember that Claude doesn't store conversation history - you need to manage it manually. When working with tool responses, you must preserve the entire content structure, including all blocks.
Here's how to properly append a multi-block assistant message to your conversation history:
messages.append({
"role": "assistant",
"content": response.content
})
Tool Result
After you've got the instructions on how to run the tool, you need to run the actual tool function and return the result back to Claude using a tool result message block.
You can also make multiple tool calls and return multiple results at once to Claude:
Follow up calls must include message history.
You also have to include the tool schemas in follow up requests to ensure Claude knows about the tool calls made.
Multi-turn Conversations with Tools
A common pattern is repeated requests from Claude to execute tools.
We use a message loop pattern to implement this:
In the response from Claude there is a stop_reason field which gets set to "tool_use" if it needs to call a tool.
Claude can also request multiple tool use in a single response - each tool block has to be processed seperatly.
The tool_result block you return back to Claude then needs to have multiple result blocks for each tool function call.
We can also respond back to CLaude with a tool error:
tool_result_block = {
"type": "tool_result",
"tool_use_id": tool_request.id,
"content": f"Error: {e}",
"is_error": True
}
Batch Tools
This is to encourage Claude to request multiple calls in one go. WE can then call the tools in parallel or async on the client side.
A batch tool accepts a list of calls to other tools - Claude then references the batch tool and provides args to say which tools it wants to run.
Existing Tool Schemas
Schema is provided, but you need to provide the impl.
- Text editing and reading
- Web search
- Structure data output