Step 1: Define Your Tools: Identify the external actions Claude needs to perform. For each action, create a 'tool' definition. This includes a unique name, a clear description of its purpose, and a JSON schema describing the required input parameters. For example, a 'get_weather' tool would need a 'location' parameter.
Step 2: Provide Tools to Claude: When making an API call to Claude, include your defined tools in the 'tools' parameter of the API request. This informs Claude about the available functions and their expected inputs.
Step 3: Prompt Claude with an Action-Oriented Request: Formulate your user prompt in a way that suggests an external action. For instance, 'What's the weather like in London?' or 'Please find my last five support tickets.' Claude will analyze this prompt against the provided tool definitions.
Step 4: Claude Invokes a Tool Call: If Claude determines a tool is needed, it will respond with a 'tool_use' message. This message contains the 'tool_name' and 'input' (arguments) as a JSON object, adhering to your defined schema. Claude does not execute the tool; it merely requests its execution.
Step 5: Execute the Tool Externally: Your application receives Claude's 'tool_use' message. Parse the 'tool_name' and 'input' from the JSON. Then, execute the actual external function (e.g., make an API call, run a database query) using the provided arguments.
Step 6: Return Tool Results to Claude: After the external tool execution, capture its output. Send this output back to Claude using a 'tool_result' message, referencing the original 'tool_use_id'. This allows Claude to incorporate the real-world data into its ongoing conversation and reasoning.
Step 7: Claude Continues the Conversation (Agentic Loop): With the tool's result, Claude can now generate a more informed response, ask follow-up questions, or even invoke another tool if the task requires further steps. This iterative process of tool invocation and result processing forms the 'agentic loop,' allowing Claude to achieve complex goals.