Section650

Writing

TopicAI
Reading8 min

Tool Engineering: Design the Actions an Agent Can Take

A tool is an operation an agent can call, and its design decides both what the agent can do and how well it chooses what to do next. Clear schemas, errors the agent can act on, permission boundaries and retry-safe operations, through one coding agent and one sorting bug.

By Ka Lun Chan · Cloud & AI · AI / Architecture

What is a tool, in an agent?

A tool is an operation the agent can call: a name, a description, the inputs it accepts and the result it returns. Its design decides what the agent is able to do, and the response it returns decides how well the agent chooses what to do next. Design tools like an API for a caller who reads every response literally and acts on it immediately.

Part of a series on what it takes to make an AI system reliable. A tool defines an operation. Whether and when the agent may call it is governed by the runtime, covered in harness engineering.

Anthropic describes tools as a contract between deterministic systems and a model whose behavior can vary. Tool definitions deserve the same attention as the main prompt. Make the valid call easy to express and reject invalid inputs explicitly, since the response informs the model’s next decision.

The tools for one bug

The illustrative scenario: a ticket says the Quantity column on the Orders page sorts as 1, 10, 2, and a coding agent is asked to fix it. The cause is that the table sorts formatted text, so “10” comes before “2” and “1,200” is a string too.

The agent needs to find code, read it, change it, test it and hand the change to a person. That is five operations.

Fig. 739-1 The sorting agent’s tools
ToolTakesReturnsSide effect
search_codeQuery, optional path prefixMatching files and lines, 20 at a timeNone
read_filePath relative to the repo root, optional line rangeThe lines requested, with line numbersNone
replace_in_filePath, exact text to replace, new textThe changed lines, or why nothing changedEdits one file in the working copy
run_testsTest file or name patternPassed, failed and skipped, with expected and actual for each failureNone outside the sandbox
open_draft_prTitle, summaryThe pull request URL, new or existingCreates a draft pull request
No shell, no merge and no push. The agent can do the job and very little else.

A general shell would cover all five and a thousand more. That is the problem with it. Anthropic’s tool guidance warns against tools that simply wrap every endpoint or capability you have, and OWASP lists excessive functionality as one of the root causes of excessive agency. Purpose-built tools are easier for the model to choose between, easier to describe, and much easier to reason about when something goes wrong.

A narrow tool set can leave the agent without a lint tool. Have it report that limitation rather than expanding shell access. Add capabilities when the task has demonstrated a need for them.

Schemas that make mistakes harder

The input schema is the first guardrail. The Model Context Protocol, for example, describes each tool with a JSON Schema for its inputs and an optional one for its output. A schema can’t make the model choose wisely, but it can make whole categories of mistakes impossible to express.

replace_in_file (input schema, illustrative)
{
  "name": "replace_in_file",
  "description": "Replace one exact block of text in a file inside the
    repository. Fails if the text is missing or appears more than once.
    Use read_file first to copy the exact text.",
  "input_schema": {
    "type": "object",
    "properties": {
      "path":     { "type": "string", "description": "Relative to the repo root, e.g. src/table/sort.js" },
      "old_text": { "type": "string", "minLength": 1 },
      "new_text": { "type": "string" }
    },
    "required": ["path", "old_text", "new_text"],
    "additionalProperties": false
  }
}

A few choices in there are deliberate. Replacing exact text, instead of rewriting the whole file or editing by line number, means a stale view of the file fails loudly instead of overwriting someone else’s change. Requiring a unique match stops an edit from landing on the wrong one of two similar blocks. The description says when to use the tool and what to do first. Anthropic’s guidance calls this poka-yoke, after the manufacturing term for mistake-proofing, and gives the example of requiring absolute file paths after a model kept getting relative ones wrong. The principle is the same here, with the opposite choice: in this repository, paths relative to the root are the ones that can’t escape it.

How does a tool response affect the agent’s next decision?

The agent decides its next step from what the tool returns. A response that names the failure, with the input that caused it and the expected and actual result, points the agent at the fix. A response that only says something failed leaves the agent to guess, and guessing agents tend to retry the same change or modify something unrelated.

Suppose the agent’s first fix converts the formatted strings with Number() before comparing them. That handles 1, 10, 2. It also turns “1,200” into NaN, because the comma isn’t a valid digit, and a blank into 0. A compare function that returns NaN can’t order anything consistently, so where those rows land depends on the order they started in. Here are two ways run_tests could report that:

run_tests response A
Tests failed (exit code 1).
run_tests response B
{
  "passed": 41, "failed": 1, "skipped": 0,
  "failures": [{
    "test": "OrdersTable sorts Quantity ascending",
    "file": "src/orders/OrdersTable.test.jsx",
    "input":    ["1,200", "10", "2", "", "5,000"],
    "expected": ["2", "10", "1,200", "5,000", ""],
    "actual":   ["1,200", "", "2", "10", "5,000"],
    "message": "Rows out of order after clicking Quantity"
  }]
}

With response A, the agent knows only that something broke. It might rerun the tests hoping for a flaky failure, read all 42 tests, or revert and try a different idea. With response B, it can see the fix works for small numbers and falls apart wherever a thousands separator or a blank appears. The obvious next move is to stop sorting display text and sort the raw value, which is the real fix.

The same applies to errors from the tool itself. The MCP specification separates protocol errors from tool execution errors, which are returned in the result with an isError flag, so the failure arrives as information the model can use. Anthropic’s guidance asks for error responses that say specifically how to correct the call. “old_text not found in src/table/sort.js; the file changed since you read it, so read it again” gets a correct retry. “Edit failed” gets a guess.

Keep responses compact, too. Anthropic recommends pagination, filtering and truncation with sensible defaults. A search that returns 4,000 matches fills the context with noise, which is a context engineering problem created by a tool.

Permission boundaries

Each tool should be able to do its job and nothing next to it. replace_in_file refuses paths outside the repository. run_tests runs the test command and nothing else. open_draft_pr opens a draft, because a draft can’t merge, and there is no merge tool at all. The credentials behind the pull request tool belong to the agent alone, scoped to one repository, so revoking them breaks nothing else.

Use deny-by-default permissions. OWASP’s guidance on excessive agency adds an important boundary: enforce authorization in the downstream system, where the model cannot override it.

Where the line falls between tool and harness is a judgment call. A tool can refuse paths outside the repository. Whether the agent may open a pull request at all before a person approves the plan is a runtime rule, and it belongs in the harness, which sees the whole run.

Retry-safe operations

Agents retry. So do runtimes, when a call times out and nobody knows whether it finished. Every tool with a side effect needs a defined answer to “what happens if this runs twice?”

  • replace_in_file: if the new text is already there and the old text is gone, report “already applied” and succeed. Failing with “text not found” sends the agent hunting for a problem that doesn’t exist.
  • open_draft_pr: use the branch as the key. If a pull request for that branch exists, return it instead of opening a second one. Payment APIs solve the same problem with idempotency keys, which Stripe describes as a way to retry a request safely without performing the operation twice.
  • run_tests and the read tools are safe to repeat by design. Say so in the description, so the agent doesn’t hesitate to rerun them.

See the production-readiness guide for the billing analogy and the consequences of retrying a tool call with side effects.

Sources

Checked in November against the documentation below. Tool names, schemas and test output are illustrative.

Short answers

What makes a good error message for an AI agent?

One that says what went wrong in terms the agent can act on: which input was invalid and why, which test failed with the expected and actual values, or which permission blocked the call. A bare exit code or stack trace leaves the agent guessing, and a guessing agent tends to retry the same thing or change something unrelated.

Should an agent tool be idempotent?

Any tool with a side effect should be safe to call twice, because agents and runtimes both retry. An edit that has already been applied should report that rather than fail confusingly or apply twice, and a tool that opens a pull request should return the existing one if it already exists.

Before you give an agent a new tool

  1. Write down the one job it does, and refuse anything outside it.
  2. Make the schema reject the mistakes you can predict.
  3. Return failures with the input, the expected result and the actual result.
  4. Decide what happens when it runs twice.
Next: harness engineering Series overview