Strategies for Forcing an LLM to Use Tools

Question: I wrote a calculator tool for an agent built in langgraph. However, the LLM keeps trying to do math itself rather than using the tool. How can I discourage it from that?

This is a common challenge with LLMs and tool calling! Here are several strategies to encourage your agent to use the calculator tool instead of doing math itself:

Prompt Engineering Approaches

1. Explicit Instructions in System Prompt Be very direct about when to use the tool:

python

2. Add Examples in Few-Shot Format Show the desired behavior explicitly:

python

LangGraph-Specific Strategies

3. Validation Node Add a node that checks if the LLM skipped the tool when it shouldn't have:

python

4. Conditional Routing Route based on whether tools were called:

python

Model Selection Considerations

5. Model-Specific Behavior Some models are better at following tool-use instructions:

For your teaching context, this is actually a great opportunity to show students the difference between models!

Debugging Tips

6. Force Tool-Only Mode Make the tool call mandatory by modifying your agent structure:

python

Teaching Opportunity

For your course, this behavior difference is actually pedagogically valuable! You could:

  1. First show the problem - let students see the LLM doing math incorrectly

  2. Demonstrate progressive solutions - from weak prompting to strong guardrails

  3. Compare models - show which models are most/least compliant

  4. Add validation layers - teach defensive programming for agent systems