How to Intermediate · 3 min read

How to force Claude to use a specific tool

Quick answer
To force Claude to use a specific tool, provide explicit instructions in the system= parameter of the client.messages.create call in the Anthropic API. This guides the model to invoke or simulate the tool usage within its responses.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the anthropic Python SDK and set your API key as an environment variable.

  • Install the SDK: pip install anthropic
  • Set your API key in your shell: export ANTHROPIC_API_KEY='your_api_key'
bash
pip install anthropic

Step by step

Use the system= parameter to instruct Claude to use a specific tool, for example, a calculator. The system prompt should clearly define the tool's behavior and when to use it.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = (
    "You are Claude, an AI assistant that must use the Calculator tool whenever a math calculation is requested. "
    "When the user asks a math question, respond only with the calculation result using the Calculator tool syntax. "
    "Do not answer math questions without using the Calculator tool."
)

messages = [
    {"role": "user", "content": "What is 123 times 45?"}
]

response = client.messages.create(
    model="claude-3-5-haiku-20241022",
    max_tokens=200,
    system=system_prompt,
    messages=messages
)

print(response.content[0].text)
output
5535

Common variations

You can customize the system prompt to force Claude to use other tools by describing their usage clearly. Also, you can adjust max_tokens or switch to other Claude models like claude-3-5-haiku-20241022. Async usage is not supported in the current SDK.

Troubleshooting

If Claude ignores the tool instructions, ensure your system= prompt is explicit and unambiguous. Also, verify you are using the correct model and that your API key is valid. If responses are incomplete, increase max_tokens.

Key Takeaways

  • Use the system= parameter to explicitly instruct Claude to use a specific tool.
  • Clear and detailed system prompts are essential to guide Claude's behavior effectively.
  • Adjust max_tokens and model choice to optimize tool usage responses.
Verified 2026-04 · claude-3-5-sonnet-20241022, claude-3-5-haiku-20241022
Verify ↗