new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Prompt Engineering · all subjects

tool use

17 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Tool use is substitution and prompt chaining

Tool use (function calling) combines substitution and prompt chaining. Claude outputs the tool name and arguments it wants to call, then halts response generation while the tool is called, then the system reprompts with the appended tool results. Function calling expands Claude's capabilities for handling complex multi-step tasks.

Tool use system prompt has two parts

A tool use system prompt consists of two parts: a general explanation of tool use and how Claude should invoke functions, and a specific tools section that lists all available tools with their parameters. The function calling structure using `<function_calls>` tags is a specific format Claude has been trained to recognize.

General tool use system prompt template

The general tool use explanation should tell Claude it has access to functions, explain the `<function_calls>` invocation format with `<invoke>` tags and `<parameter>` elements, note that strings and scalars are specified as-is and lists/objects use JSON format, and explain that spaces in string values are not stripped and the output is parsed with regular expressions.

Specific tools system prompt format

Specific tools are defined in a `<tools>` section containing `<tool_description>` elements. Each tool has a `<tool_name>`, `<description>`, and `<parameters>` section. Each parameter has a `<name>`, `<type>`, and `<description>`. This structure should list all tools and parameters Claude has access to in that specific scenario.

Stop sequences for tool use detection

Use `stop_sequences=['</function_calls>']` when calling Claude for tool use. This causes Claude to halt response generation when it emits the closing tag, allowing you to extract and execute the function before continuing the conversation.

Function results formatting for Claude

Tool results must be formatted in a specific structure that Claude recognizes: `<function_results>` containing `<result>` blocks, each with `<tool_name>` and `<stdout>` tags. The format is: `<function_results>\n<result>\n<tool_name>{TOOL_NAME}</tool_name>\n<stdout>\n{TOOL_RESULT}\n</stdout>\n</result>\n</function_results>`

Tool use conversation flow with message history

After Claude makes a tool call, append it to the messages as an assistant message (with the closing `</function_calls>` tag), then add a user message containing the formatted tool results. Pass this complete message history to Claude in the next request to continue the conversation with the tool results context.

Claude skips tools when not needed

Claude will correctly decline to use tools when the user's question does not require them. For example, if asked 'Tell me the capital of France' when tool use is available, Claude will simply answer the question without calling any functions.

Calculator tool example with parameters

A calculator tool example has tool_name 'calculator', description 'Calculator function for doing basic arithmetic. Supports addition, subtraction, multiplication', and three parameters: first_operand (int, 'First operand (before the operator)'), second_operand (int, 'Second operand (after the operator)'), and operator (str, 'The operation to perform. Must be either +, -, *, or /'). This demonstrates the structure for defining parameterized tools.

Database query tool use exercise structure

The SQL exercise in the tutorial demonstrates tool use with four database functions: get_user(user_id), get_product(product_id), add_user(name, email), and add_product(name, price). Each function requires parameters to be defined in the system prompt with name, type, and description. The exercise tests whether Claude can correctly extract parameters and call functions like 'Add a user to the database named Deborah' and 'Tell me the name of User 2'.

Claude 3 Sonnet is generally better at tool use

Claude 3 Sonnet (model 'claude-3-sonnet-20240229') is generally better at tool use compared to other models, as noted in the tutorial setup.

Future tool use improvements mentioned

Anthropic noted that tool use functionality will be updated and improved in the near future with a more streamlined format for function definitions and calls, more robust error handling and edge case coverage, tighter integration with the API, and better reliability and performance for complex tool use tasks.

Extract parameters from Claude's function call response

To extract parameters from Claude's function call XML response, search for the parameter name pattern `name="{parameter_name}">` and extract the text between that tag and the closing `<` character. This allows you to parse Claude's tool invocation and retrieve the values it provided for each parameter.

Claude can search Wikipedia and retrieve articles

Claude can search through Wikipedia for relevant articles and retrieve them. After retrieval, you can use Claude to summarize and synthesize the articles, write novel content based on what was found, and perform other tasks with the retrieved information.

Claude can search over custom documents and vector datastores

Beyond Wikipedia, you can use Claude to search over your own documents whether stored as plain text or embedded in a vector datastore. This allows you to supplement Claude's knowledge and improve the accuracy and relevance of its responses with retrieved data.

RAG with Claude for vector databases and external data sources

Anthropic provides RAG cookbook examples demonstrating how to use Claude with vector databases, Wikipedia, the internet, and embeddings to retrieve and augment responses. The cookbook shows how to use embeddings and vector database tools to enhance Claude's capabilities.

Advanced RAG architectures available for Claude

Anthropic provides technical presentation slides on advanced RAG (Retrieval-Augmented Generation) architectures using Claude 3, available in their documentation for those interested in learning sophisticated approaches to retrieval and augmentation.

Give your agent this brain