Lesson

Forcing Claude to Use a Tool

By default, when you provide a tools array to Claude, it evaluates the user's prompt and decides on its own whether to use a tool or just answer with text. This is called tool_choice: "auto".

But sometimes, you don't want Claude to have a choice.

The tool_choice Parameter

If you are building an application where the only acceptable outcome is executing a specific function (like extracting data to a database), you can force Claude's hand using the tool_choice parameter.

const response = await anthropic.messages.create({
  model: 'claude-3-haiku-20240307',
  max_tokens: 500,
  tools: [
    {
      name: "save_to_database",
      description: "Saves a user's name and age.",
      input_schema: { /* ... */ }
    }
  ],
  tool_choice: { type: "tool", name: "save_to_database" }, // Forcing the tool
  messages: [
    { role: 'user', content: 'My name is Bob and I am 40.' }
  ]
});

Because we explicitly set tool_choice to save_to_database, Claude will completely bypass its normal conversational logic and immediately output a tool_use block formatted for that specific function.

Why Force a Tool?

Forcing a tool is the ultimate way to guarantee structured data extraction.

In a previous lesson, we used the "Assistant Prefilling" trick to force JSON output. Forcing a tool achieves a very similar result, but it is often cleaner and more robust for complex data structures.

If you force Claude to use save_to_database, it is mechanically required to output the input arguments (like name and age) in perfect JSON that matches your schema.

'Any' Tool Choice

There is a third option for tool_choice.

  • auto: Claude decides if a tool is needed. (Default)
  • tool: Claude MUST use the specific tool you name.
  • any: Claude MUST use a tool, but it can choose which tool from the array you provided.

If you provide three tools (get_weather, get_time, get_news) and set tool_choice: { type: "any" }, Claude will refuse to answer conversationally. It will force itself to pick at least one of those three tools based on the user's prompt.

Understanding how to manipulate tool_choice gives you ironclad control over Claude's output behavior in production apps.

Ready to test your understanding? Take the quiz to reinforce what you learned.

© 2026 Ant Skillsv.26.07.07-23:01