Completing the Tool Use Cycle

Introduction & Overview

In the previous lessons, you learned how to create tool schemas and understand Claude's responses when it wants to use tools. You can now recognize when Claude requests tool execution through the stop_reason: "tool_use" signal and extract the necessary details from tool use blocks. However, knowing what Claude wants to do is only half the story — you still need to actually execute those tools and complete the conversation cycle.

In this lesson, you'll learn how to bridge that gap by executing the functions Claude requests, capturing their results, and sending those results back to Claude in the proper format. By the end of this lesson, you'll have a complete tool execution pipeline that can handle Claude's tool requests from start to finish, maintaining proper conversation flow throughout the entire process.

The Complete Tool Execution Flow

Before we dive into the implementation, let's understand the complete workflow we'll be building in this lesson. Here's the step-by-step process that transforms Claude from a simple chatbot into a capable agent:

  1. Set up the foundation - Create function mappings, tool schemas, and initial conversation messages
  2. Send the initial request - Make the first API call to Claude with the user's question and available tools
  3. Detect tool use requests - Check Claude's response for the "tool_use" stop reason
  4. Extract tool information - Pull out the function name, parameters, and unique ID from each tool use block
  5. Execute the requested functions - Use our function mapping to call the actual TypeScript functions with Claude's parameters
  6. Collect and format tool results - Structure all function outputs in the specific format Claude expects
  7. Send results back to Claude - Make a second API call with the complete conversation, including tool results
  8. Display the final response - Show Claude's natural language answer that incorporates the tool outputs

This complete cycle enables Claude to seamlessly use tools as part of its reasoning process, transforming raw function outputs into conversational responses that directly answer user questions.

Setting Up the Foundation

Before diving into tool execution, we need to establish the foundation that connects Claude's tool requests to our actual TypeScript functions. As we covered in previous lessons, this involves creating a mapping object and preparing our tool schemas and initial messages. The critical component here is the function mapping object — this serves as the bridge between the tool names Claude uses and our actual TypeScript functions.

import fs from 'fs';
import Anthropic from "@anthropic-ai/sdk";
import { sumNumbers, multiplyNumbers } from './functions';

// Initialize the Anthropic client
const client = new Anthropic();

// Create an object mapping tool names to functions
// This is crucial - the keys must match our tool schema names exactly
const tools: Record<string, Function> = {
    "sum_numbers": sumNumbers,
    "multiply_numbers": multiplyNumbers
};

// Choose a Claude model
const model = "claude-sonnet-4-6";

// System prompt with explicit instructions to use tools
const systemPrompt = 
    "You are a helpful math assistant. " +
    "Always use the available tools to perform calculations accurately.";

// Load the schemas from JSON file
const schemasJson = fs.readFileSync('schemas.json', 'utf-8');
const toolSchemas = JSON.parse(schemasJson);

// Create an array of messages to send to Claude
const messages: Anthropic.MessageParam[] = [
    { role: "user", content: "Please calculate 15 + 27" }
];

This setup creates everything we need for tool execution: the tools object enables dynamic function lookup, the systemPrompt guides Claude's behavior, the toolSchemas provide technical specifications, and the messages array starts the conversation. The function mapping is particularly important because it allows our code to execute the correct function based on Claude's string-based tool requests.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal