Tool Discovery and Lazy Loading for Scalable AI Agents

    As an agent’s tool library grows, sending every tool schema in every request increases input cost, latency, and selection errors. This article explains how to separate a lightweight tool catalog from on-demand tool definitions, covering discovery flows, naming, versioning, authorization, caching, observability, and practical rollout checks.

    When business operations such as querying, writing, approvals, retrieval, and external-system actions are exposed as callable tools, an AI agent no longer works with a small, fixed prompt. It must operate against a tool library that grows with every release.

    If every request includes all parameter schemas, enum values, and examples, both the input size and the number of distracting candidates increase. The model must identify intent within a longer context, the gateway must transmit more repeated text, and retries may send the same definitions again. Tool discovery addresses this by separating two tasks:

    1. Knowing which tools are available.
    2. Loading the complete definition of the tool needed for the current task.

    The full definition is loaded only when it is relevant.

    Three Costs That Appear at Scale

    1. Input cost

    A tool definition typically includes its name, purpose, parameter types, required fields, value constraints, and usage examples. A single definition may be small, but hundreds of them repeated across multiple conversation turns can consume context that should be reserved for task state, user constraints, and intermediate results.

    When billing is based on input size, unused tools become a predictable fixed cost.

    2. Latency

    The longer the definitions are, the more time is required to assemble, transmit, and prefill the request. For tasks involving several consecutive tool calls, the delay introduced in the first turn can affect the entire workflow.

    From the user’s perspective, the result is often not that “the model is slower,” but that there is a noticeable pause between each question and answer.

    3. Tool selection quality

    As the candidate list grows, tools with similar names or vague descriptions become easier to confuse. The model may select an interface with overlapping functionality but different permissions, or fill in missing parameters with guesses.

    The cost of lower accuracy—including rework, compensating calls, and human intervention—can exceed the savings from removing a few definitions from the request.

    The goal is therefore not simply to remove tools. It is to ensure that the model can always see the boundary of available capabilities without placing irrelevant parameter schemas in the current request.

    The Mechanism: A Ready Catalog and On-Demand Definitions

    A tool library can be divided into two layers.

    Catalog layer

    The catalog contains stable, compact information:

    • Tool name
    • One-sentence responsibility
    • Appropriate use cases
    • Cases where the tool should not be used
    • Whether lazy loading is enabled

    The model uses this information to determine whether a tool is relevant. The catalog must cover every callable tool. Otherwise, the model may assume that a capability does not exist.

    Definition layer

    The definition layer contains parameter schemas, return structures, and invocation constraints. These definitions are excluded from the request by default. They are added only when the current task matches a tool or when the model explicitly requests a definition by name.

    The model can then generate a schema-compliant call in the same context. Ideally, discovery and execution happen within one round trip. This avoids the additional latency of first looking up a definition and then sending a second request.

    A small number of tools that are always on the hot path—such as identity confirmation and task-status checks—can remain immediately loaded. Long-tail tools, such as specialized reports, occasional approval actions, and less common data sources, can be marked for lazy loading.

    The classification should be based on invocation frequency, definition size, and misselection risk—not simply on business department.

    The following flow is conceptual. The field names do not represent any specific platform interface:

    1. Request
    2. Catalog: [name, summary, lazy-loading status]
    3. Immediate definitions: complete schemas for hot-path tools only
    4. Discovery action: retrieve the definitions of one or more lazy tools by name
    5. If discovery succeeds
    6. Add the definitions to the same request context
    7. Validate parameters and execute the call
    8. Write the result back to conversation state
    9. If discovery fails or the description is insufficient
    10. Return a shorter list of possible tools and ask for clarification
    11. Never invent a tool name that is not present in the catalog

    What Happens During a Request

    When building a request, the application includes the tool catalog and marks long-tail tools as lazy. Their full parameter schemas are not expanded into the prompt.

    The model first reads the user’s objective and the catalog. If the available immediate tools are sufficient, it calls one directly. If a long-tail capability is required, it requests the definition using the exact name listed in the catalog.

    The server then verifies that the name belongs to the set of tools available to the current principal, loads the definition, and allows the model to generate schema-compliant parameters in the same context before execution.

    This design imposes three engineering requirements.

    Use stable, unique names

    Names must be stable and unique. Discovery keys should not be based on display text, which may change over time.

    Make descriptions distinguishable

    Descriptions must clearly separate similar tools. Avoid generic phrases such as “query data” or “process request” for multiple tools.

    Enforce authorization during loading and execution

    A tool appearing in the catalog does not necessarily mean that the caller is allowed to execute it. The server must still enforce identity, tenant, and parameter-level authorization.

    Failures should also be explainable. A missing name, expired definition version, parameter validation failure, and downstream timeout should be returned as different error categories rather than as a generic “tool unavailable” response.

    Otherwise, the model may repeatedly retry with similar names, creating a call storm.

    How to Write Tool Descriptions That Support Discovery

    Discovery quality depends heavily on catalog text, not only on model size. Each tool should clearly state at least four things:

    1. What it does
    2. Which business object it accepts as input
    3. When it should not be used
    4. What the user receives after a successful call

    For similar tools, use contrastive descriptions. For example, “look up status by order ID” and “find a user’s recent orders” are less likely to be confused than two tools both described as “order lookup.”

    Keep parameter schemas flat and types explicit. Minimize required fields where possible. If tenant or operator information can be inferred from conversation state, do not require the model to provide it again.

    When an enum contains many values, the definition can reference a versioned vocabulary instead of embedding every historical value in each request. Keep only the shortest successful path in the example so that the example does not become another source of context overhead.

    Tool changes should be versioned. Keeping the same name while changing parameter semantics is a common cause of production incidents. Lazy loading makes old definitions less likely to be visible in every request, which makes it even more important to verify the definition version before execution and reject calls generated from an outdated schema.

    Coordinating Discovery with Context, Caching, and Observability

    Tool discovery reduces repeated definitions, not all context. Multi-turn tasks still need to retain the objective, confirmed slots, and summaries of previous tool results.

    If a complete result is very large, store it externally and provide the model with a summary and reference. Otherwise, the result can quickly fill the context that lazy loading just saved.

    The catalog is suitable for short-lived caching because names and summaries usually change less frequently than conversation content. Definition caches must be bound to version numbers.

    Permission changes and retired tools must be able to invalidate caches immediately. Otherwise, the model may discover a capability that is no longer authorized or has already been deprecated.

    At minimum, observability should record:

    • Catalog size
    • Number of definitions loaded immediately in each request
    • Number of discovery attempts
    • Tools selected
    • Parameter validation failure rate
    • Average additional latency
    • Whether the final call succeeded

    Total input size alone is not enough. You must distinguish catalog overhead from definition overhead to determine whether the hot-path classification is correct.

    If discovery itself fails frequently, improve descriptions and tool boundaries before adding more tools.

    When Lazy Loading Is Not Appropriate

    Tools that are time-sensitive, used in every turn, or have very short definitions usually provide little benefit when lazily loaded. Lazy loading may only add another decision step.

    Security-related rejection rules and identity checks must be enforced by the server. Do not rely on the model to remember to load them from a catalog.

    For write operations that require strict auditing, lazy loading can still be used for the definition, but the system must recheck the principal, idempotency key, and impact scope immediately before execution.

    If tools have constraints that require them to be visible together—for example, a create operation and its compensating cancellation operation—make that relationship explicit in the catalog or policy. This prevents the model from seeing only one side of the pair.

    When discovery returns multiple candidates, limit the number of definitions that can be loaded in a single turn. Otherwise, a request may pull the entire long tail back into context.

    Implementation Checklist

    Before deployment, review the following steps rather than switching every tool to lazy loading at once:

    • Measure invocation frequency, definition size, and misselection history for each tool. Use the results to identify hot-path and long-tail tools.
    • Rewrite names and summaries so that similar tools can be distinguished in one sentence.
    • Make the catalog visible to the model while keeping complete schemas out of long-tail requests by default.
    • Keep discovery, authorization, parameter validation, and execution within one controlled path. Do not allow clients to bypass it by constructing calls directly.
    • Define retry behavior separately for unknown names, authorization failures, expired versions, validation failures, and downstream timeouts.
    • Limit the number of definitions loaded per turn and apply backoff to repeated discovery attempts.
    • Compare real multi-turn tasks using input size, end-to-end latency, first-choice accuracy, and human handoff rate.

    Tool discovery does not reduce an agent’s capabilities. It separates capability visibility from definition weight.

    The catalog ensures that the model understands the available boundary, while on-demand loading ensures that the current request pays the context and latency cost only for the parameters it actually needs. With disciplined naming, versioning, authorization, and observability, agents can continue to scale their tool libraries while maintaining predictable cost and selection quality.