Tool Use Patterns

Production-grade guide to tool use patterns covering architecture patterns, implementation strategies, testing approaches, and operational best practices for enterprise engineering teams.

Tool Use Patterns are the deliberate, repeatable strategies engineers use to integrate external tools into agent workflows—tools that extend the agent’s capabilities beyond its core logic. These patterns matter when agents must act on data, manipulate systems, or execute tasks that no single tool can handle alone. They emerge in scenarios where an agent must call a service, process a file, trigger a pipeline, or orchestrate a workflow across diverse systems, and the success of the agent hinges not just on *what* tools it uses, but *how* it uses them.

Selecting and Invoking Tools

When an agent must choose among multiple tools, the decision is not merely about capability but about performance, availability, and fidelity. The agent evaluates tools based on interface, response time, error semantics, and context requirements.

The most common selection mechanism is a tool_call message in a structured request, using a standardized format such as:

{
  "tool_calls": [
    {
      "id": "call_123",
      "type": "function",
      "function": {
        "name": "send_email",
        "arguments": {
          "to": "[email protected]",
          "subject": "Meeting Notes",
          "body": "See attached."
        }
      }
    }
  ]
}

The agent must resolve the tool_name to a real endpoint, typically via a registry or catalog. This registry may be a simple JSON file, a database table, or a REST API returning a list of tools with metadata:

GET /tools
Accept: application/json
Authorization: Bearer <token>
[
  {
    "name": "send_email",
    "description": "Sends an email via SMTP",
    "input_schema": {
      "type": "object",
      "properties": {
        "to": { "type": "string", "format": "email" },
        "subject": { "type": "string" },
        "body": { "type": "string" },
        "cc": { "type": "array", "items": { "type": "string" } }
      },
      "required": ["to", "subject"]
    },
    "output_schema": {
      "type": "object",
      "properties": {
        "message_id": { "type": "string" },
        "status": { "type": "string", "enum": ["sent", "failed", "queued"] }
      }
    },
    "url": "https://mail-service.example.com/v1/send"
  }
]

The agent selects a tool and invokes it via POST to the provided url, passing arguments in the request body. The tool returns a 200 OK with a result object, or 4xx/5xx on failure.

Common Pitfalls in Tool Invocation

  {
    "error": "Invalid input",
    "details": [
      "Field 'files.0.content' is required",
      "Field 'files.1.type' must be a string"
    ]
  }

Handling Tool Responses and Errors

A tool’s response may be immediate or asynchronous. The agent must handle both.

For synchronous tools, the agent waits for the response within a configured timeout, typically 5s to 30s. If the tool responds with 200 OK, the agent extracts the result field and proceeds.

For asynchronous tools, the agent receives a 202 Accepted and must poll for completion:

POST /tools/send_email
{
  "to": "[email protected]",
  "subject": "Meeting Notes",
  "body": "See attached.",
  "async": true
}
HTTP/1.1 202 Accepted
Location: https://mail-service.example.com/v1/jobs/987
Content-Type: application/json

{
  "job_id": "987",
  "status": "queued",
  "message": "Email submission received, processing will begin."
}

The agent then polls GET /jobs/987 until status is completed or failed. It may use exponential backoff:

curl -H "Authorization: Bearer <token>" https://mail-service.example.com/v1/jobs/987
{
  "job_id": "987",
  "status": "processing",
  "progress": 60,
  "updated_at": "2025-04-05T10:15:30Z"
}

The agent must detect job_id in the tool_call and store it for later lookup. A common failure occurs when the agent assumes job_id is always returned in the top-level response but receives it only in the data field, leading to missed or misattributed jobs.

Error Handling Patterns

Tool errors fall into three categories:

  1. Client errors (4xx): The agent’s input was malformed or incomplete.
  2. Server errors (5xx): The tool failed internally, possibly transiently.
  3. Business logic errors: The tool executed but returned a domain-specific error.

The agent must distinguish these and react accordingly.

For example, a tool that generates a PDF report may return:

{
  "status": "error",
  "error": {
    "code": "pdf_generation_failed",
    "message": "Failed to render report due to missing template.",
    "details": {
      "template_id": "report_v1",
      "missing_assets": [
        "logo.png",
        "styles.css"
      ]
    }
  }
}

The agent must parse this error and either retry with a fallback template or trigger a job to regenerate the missing assets. A subtle but frequent mistake is assuming that error.message is always sufficient, but the agent ignores details, leading to silent failures when a template is missing but the error message says only “PDF generation failed.”

Composing Tools into Workflows

When multiple tools are needed, the agent composes them into a sequence or a graph. The agent must manage the order, dependencies, and shared state across tools.

A common pattern is to use a tool_sequence message:

{
  "tool_sequence": [
    {
      "tool": "send_email",
      "arguments": {
        "to": "[email protected]",
        "subject": "Report Generated",
        "body": "Your report is ready."
      }
    },
    {
      "tool": "upload_file",
      "arguments": {
        "file": "report.pdf",
        "destination": "/reports/2025/q2/"
      }
    },
    {
      "tool": "notify_slack",
      "arguments": {
        "channel": "#reports",
        "text": "Report uploaded and emailed.",
        "attachments": [
          {
            "title": "Report Summary",
            "fields": [
              { "title": "Size", "value": "2.1 MB" },
              { "title": "Generated by", "value": "agent-01" }
            ]
          }
        ]
      }
    }
  ]
}

The agent executes each tool in order, passing context (e.g., job_id, file_id) between steps.

Shared State and Context Passing

The agent maintains a context object, a JSON map of values that persist across tool calls. The agent may populate this context before invoking tools and update it from tool results.

For example, after send_email, the agent receives:

{
  "message_id": "msg_abc123",
  "status": "sent"
}

It updates the context:

{
  "email": {
    "message_id": "msg_abc123",
    "status": "sent"
  },
  "report": {
    "file_id": "report.pdf",
    "size": 2147483648
  }
}

The next tool can then use context.report.file_id as input.

A common failure is context key mismatch: the agent expects context.report.file_id but the previous tool sets report.fileId (camelCase) or report.file_id (snake_case), and the agent fails silently. The tool upload_file receives file: "report.pdf" but the agent expected file_id.

Asynchronous Composition and Fan-Out

In complex workflows, the agent may launch multiple tools in parallel and wait for all to complete.

For instance, when generating a weekly report, the agent:

  1. Queries a database for data.
  2. Calls generate_pdf for each report section.
  3. Uploads all generated PDFs.
  4. Merges them into a single file.

Each generate_pdf call returns a job_id. The agent collects all job_ids and waits for all to finish, using a wait_for_jobs tool:

{
  "tool": "wait_for_jobs",
  "arguments": {
    "job_ids": [
      "job_1",
      "job_2",
      "job_3"
    ],
    "timeout": 60,
    "poll_interval": 5
  }
}

The agent polls each job until all are completed or the timeout is reached. If one job fails, the agent must decide whether to continue or abort the entire workflow.

Fan-Out and Fan-In Patterns

The agent may fan out to multiple tools, each processing a subset of data. For example, to process a batch of customer orders:

{
  "tool": "process_order_batch",
  "arguments": {
    "order_ids": [1001, 1002, 1003, 1004]
  }
}

The agent creates a job for each order and sends each to a process_order tool. After all are dispatched, it waits for all to complete.

The fan-in step collects results:

[
  {
    "order_id": 1001,
    "status": "processed",
    "invoice_url": "https://invoices.example.com/1001"
  },
  {
    "order_id": 1002,
    "status": "failed",
    "error": "Payment declined"
  }
]

The agent then aggregates these into a summary, storing the result in the context.

A common failure is result aggregation mismatch: the agent expects results[i].order_id but receives results[i].id, or the agent assumes status is always a string but receives status: 200 (integer). The agent fails to map the results correctly, and the next step uses stale or incomplete data.

Reusing and Managing Tool Libraries

As agents grow, they accumulate a library of tools. The agent must manage versioning, discovery, and reuse.

Tool versions are specified in the tool_call:

{
  "tool": "send_email",
  "version": "v2.1",
  "arguments": {
    "to": "[email protected]",
    "subject": "Meeting Notes"
  }
}

The agent resolves send_email/v2.1 to a specific endpoint, possibly with a versioned API path or a header:

GET /tools/send_email/v2.1

Tool libraries may be versioned in a registry that supports GET /tools?version=v2.1 and returns a list of tools available in that version.

The agent may also cache tool metadata locally, reducing the number of calls to the registry. A common failure is cache invalidation: the agent updates a tool in the registry but fails to invalidate the local cache, leading to outdated tool descriptions and unexpected behavior.

Failure Modes and Observability

The agent must detect and react to tool failures:

The agent must log detailed trace information: the tool_call.id, the input arguments, the tool endpoint, the response, and any intermediate state. These logs allow debugging tool use patterns across multiple agents and environments.

Summary of Key Patterns

These patterns are not optional—they are the foundation of agent-driven systems that interact with the world beyond their code.

This page was rewritten on 13 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.