Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Tool Selection Logic for AI Agents

Hello! Welcome to our next lesson.

In our previous session, we established the foundation for agent action by learning how to define a single tool, write a precise schema for it, and craft clear descriptions to guide the agent's usage. We treated the tool schema as a form of "governance" that structures the agent's interaction with the world.

Today, we address the next level of complexity, which directly targets the learning outcome: Design tool selection logic so an agent chooses the right tool for a given sub-task. When an agent has access to multiple tools, the decision of which one to use becomes a critical point of failure or success. We will analyze different architectural patterns for this decision-making process, evaluating their trade-offs in terms of reliability, control, and suitability for the production-grade client solutions you aim to build.

1. The Default Approach: Probabilistic Tool Selection

When you provide an LLM with a list of available tools, the default mechanism in most modern APIs relies on the model's own reasoning to select the appropriate one. The model analyzes the user's query and the descriptions of all available tools, then makes a probabilistic decision about which tool (if any) to invoke.

For systems with a very large number of tools, this process can be refined into a two-step "routing" or "selection" mechanism to manage the context window and focus the model's attention.

Stanford CME295 Transformers & LLMs | Autumn 2025 | Lecture 7 - Agentic LLMs

This lecture segment from Stanford provides a clear, conceptual overview of this challenge and introduces the idea of a 'tool selector' or 'router'.

Please watch the segment from 01:22:26 to 01:28:31. Focus on these key points: The problem that arises when you have multiple tool APIs available. The issue of the finite context window and the 'needle in a haystack' problem. The two-step process proposed by the Google DeepMind paper: a 'tool selector' or 'router' first restricts the list of tools, and a second call uses the filtered list.

This two-step process—filter then execute—is a common pattern. An initial LLM call acts as a router, identifying a small subset of potentially relevant tools. A second LLM call then receives this smaller, more focused list to make the final selection and determine the parameters.

While this approach leverages the LLM's impressive reasoning capabilities and is straightforward to implement, it carries a significant architectural drawback: the core decision-making is probabilistic. This can lead to unpredictable and inconsistent behavior, which is often unacceptable in business-critical workflows.

2. The Reliability Problem: When Probabilistic Selection Fails

For the systems you intend to build for clients, "it works most of the time" is not a sufficient guarantee. Relying on purely probabilistic tool selection introduces a large "surface area of failure." To build robust systems, we must first understand these failure modes in detail.

Architectural Limitations of Probabilistic Tool Selection

This article, 'Architectural Limitations of Probabilistic Tool Selection,' provides a sharp analysis of why this approach can be problematic in production. It contrasts the standard tool-calling method with a more deterministic 'code-first' approach.

Please read the following sections: Abstract & Analysis: Understand the core argument and the three failure patterns identified: inconsistent problem-solving, interpretation errors, and logical mistakes. Q1: 'Pick That Ping-Pong' Riddle: Note how the agent fails due to 'inconsistent delegation'—choosing to reason qualitatively when a computational tool was available and necessary. Q2 & Q3 Analysis: Observe how 'opaque reasoning' and 'logical errors' lead to incorrect results, even when the agent seems to be on the right track. Real-World Business Complexity: Pay close attention to how these isolated failures compound in a complex business query, making the entire process unreliable. Focus on the architectural implications of these failures for building predictable client solutions.

The key takeaway is that when the tool selection logic is embedded within the LLM's probabilistic reasoning, you lose a significant degree of control. The system becomes difficult to debug and its behavior can be inconsistent. As the article highlights, even if the right tools are available, consistent delegation remains unreliable. This forces us to consider architectures that impose more determinism on the workflow.

3. A Deterministic Alternative: Intent Classification & Routing

To build more reliable systems, we can separate the task of understanding intent from the task of selecting a tool. This leads to a powerful architectural pattern:

  1. Classify Intent: Use the LLM for what it does best: language understanding. Give it the user's query and ask it to classify the intent into one of a set of predefined categories, using the structured output capabilities we discussed in the previous lesson.
  2. Route with Code: In your application's business logic, use this classified intent to deterministically route the request to the correct tool. This is done with standard, predictable code (e.g., if/else statements, a dictionary mapping intents to functions).

This approach moves the critical routing logic out of the probabilistic LLM and into your deterministic, testable, and auditable codebase.

How to Build Reliable AI Agents (without the hype)

This video provides a clear, practical demonstration of this intent-and-route pattern. It directly contrasts this method with native tool calling and explains the production benefits.

Watch the segment from 16:52 to 21:30. The presenter explains why they rarely use native tool calls in production. Pay close attention to: How they use structured output to classify a message's 'intent' (question, request, complaint). How simple if/else statements in the code act as a router based on this intent. The critical advantage for debugging: the LLM's reasoning for its classification choice is captured, creating a clear audit trail.

This pattern, which the PromptQL article would call a "code-first" approach, offers the best of both worlds. You use the LLM's semantic understanding while retaining full control over the workflow's execution path. For the services you plan to offer, this provides a much stronger foundation for reliability and maintainability.

4. Advanced Strategy: Concurrent and Optimized Tool Calling

We can extend the deterministic routing concept to handle more complex scenarios. What if multiple tools can serve the same purpose (e.g., two different weather APIs, or both a database lookup and a web search for product information)? Instead of choosing just one, a more robust strategy might be to call several and compare the results.

Recent research explores architectures that formalize this idea, adding another layer of classification and optimization.

A study on classification based concurrent API calls and optimal ...

This research paper proposes a workflow that classifies tools by purpose and enables concurrent calls. It also investigates the optimal combination of LLM models for different stages of the process.

Read the following sections from the paper: Methods > Work flow: Understand the proposed three-stage architecture (Planner, Worker, Solver) and the role of the 'Tool Classifier' and 'Multi Calling Prompt'. Methods > Classification criteria: Grasp the design consideration of how broadly or narrowly to classify tools. Experiment > Model combination optimization: This is the most important part. Absorb the counter-intuitive finding: using a less-advanced model (GPT 3.5t) for the 'Planner' (the routing step) and a more-advanced model (GPT 4o) for the 'Solver' (the synthesis step) often yielded the best results. The analysis suggests the older model was more 'controllable' and less likely to deviate from the provided examples.

This paper delivers several profound insights for system architecture:

  • Tool Classification: You can design your system to first categorize its tools (e.g., 'Information Retrieval', 'Task Completion'), then decide which category of tool is needed.
  • Concurrent Execution for Redundancy: For a given task, you can invoke multiple tools of the same type simultaneously. This builds resilience; if one tool fails or returns a poor result, another might succeed.
  • Optimized Model Selection: The most advanced model isn't always the best tool for every job. For a constrained task like routing, a less powerful (and cheaper) model that adheres more strictly to instructions can be more reliable than a highly creative one. This allows you to create a "cost vs. capability" strategy, using powerful models only where their reasoning is truly needed.

Conclusion

In this lesson, we dissected the critical task of tool selection. We moved beyond the default probabilistic approach to design more robust, predictable, and auditable systems suitable for production environments.

Key Takeaways:

  • Probabilistic vs. Deterministic: Standard tool-calling is probabilistic and can be unreliable. A more robust architecture uses the LLM to classify intent and then uses deterministic code to route to the correct tool.
  • Intent-and-Route Pattern: This is a cornerstone of reliable agent design. It provides control, observability, and debuggability by separating semantic understanding from workflow execution.
  • Advanced Architectural Choices: For further optimization, you can classify tools by type, call them concurrently for redundancy, and use different LLM models for routing (planning) versus synthesis (solving) to balance cost, control, and performance.

You now have a portfolio of architectural patterns for designing an agent's decision-making logic. In our next lesson, we will address the inevitable reality of system failures. We'll explore how to design retry and error-handling patterns for when tool calls fail or return unexpected results, ensuring your agentic workflow is not just effective, but resilient.

Can't find a good explanation? Sign up and we'll make it for you

Sign up