CC-301l · Module 2

Tool Use via API

3 min read

Tool use is the API feature that enables Claude to call functions in your code. You define tools with JSON Schema descriptions — the same pattern as MCP, but embedded directly in the API call. When Claude determines it needs to use a tool, it returns a tool_use response block instead of text. Your code executes the tool, sends the result back, and Claude continues its response incorporating the tool result.

The tool use loop: send messages with tool definitions → Claude responds with tool_use → your code executes the tool → send the tool_result back to Claude → Claude responds with text. This loop can repeat — Claude can use multiple tools in sequence, with each tool result informing the next decision. This is the same mechanism Claude Code uses internally for reading files, running commands, and editing code. The API exposes it for your custom tools.

const response = await client.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  tools: [{
    name: 'get_ticket',
    description: 'Get a support ticket by ID',
    input_schema: {
      type: 'object',
      properties: { ticket_id: { type: 'string' } },
      required: ['ticket_id'],
    },
  }],
  messages: [{ role: 'user', content: 'Summarize ticket SUPPORT-1234' }],
});

// Claude returns: { type: 'tool_use', name: 'get_ticket', input: { ticket_id: 'SUPPORT-1234' } }
// Execute the tool, send result back, Claude produces the summary.