← Agent 探索

01.16 / Tooling / EN

A Practical Tool-Use Checklist

How to design tool contracts, authorization, idempotency, verification, and recovery.

Tools are the boundary where an agent stops proposing text and starts changing the world. That boundary deserves the same engineering discipline as a payment API, deployment system, or database migration.

A function name and a JSON Schema are not enough. A production tool must define what the operation means, who may run it, which effects it can create, how failures are classified, and how the final state is verified.

This guide treats every tool call as a small transaction:

intent
  → validate
  → authorize
  → prepare
  → execute
  → verify
  → commit trace

If any stage is missing, the agent may still work in a demo. The missing stage will eventually become an incident in production.

1. Start with the effect, not the function signature

Before defining parameters, classify what the tool can do.

Class Examples Default control
Read Search documents, inspect a task Auto-execute with audit
Reversible write Create a draft, update a label Policy check and verification
External communication Send email, post a message Preview and scoped confirmation
Irreversible write Delete data, publish, transfer funds Explicit checkpoint and strong identity

Risk depends on more than the HTTP method. A GET endpoint can expose sensitive data; a reversible write may still affect thousands of records. Evaluate:

The resulting risk class should travel with the tool definition and appear in traces, policy decisions, and evaluation results.

2. Define a complete contract

Structural contract

Use a strict schema:

{
  "type": "object",
  "additionalProperties": false,
  "required": ["calendar_id", "title", "start_at", "end_at"],
  "properties": {
    "calendar_id": {
      "type": "string",
      "pattern": "^cal_[a-z0-9]+$"
    },
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "start_at": {
      "type": "string",
      "format": "date-time"
    },
    "end_at": {
      "type": "string",
      "format": "date-time"
    },
    "attendee_ids": {
      "type": "array",
      "maxItems": 20,
      "items": {
        "type": "string"
      }
    }
  }
}

Schema validation should happen before authorization and execution. The downstream service should never receive an argument set that the harness already knows is invalid.

Semantic contract

JSON Schema cannot express all business rules. Add deterministic validators for relationships such as:

Do not delegate rules with known answers to the model. Deterministic checks are cheaper, more stable, and easier to audit.

Result contract

The result should separate business data from execution metadata:

{
  "status": "accepted",
  "data": {
    "event_id": "evt_42"
  },
  "execution": {
    "request_id": "req_91",
    "idempotency_key": "task_8f2a:create_event:v1",
    "started_at": "2026-09-30T10:18:22Z",
    "duration_ms": 184
  }
}

Avoid returning ambiguous strings such as "done" or "something went wrong". The harness needs machine-readable states.

3. Make authorization contextual

Tool availability and tool authorization are different.

The model may know that delete_document exists, but execution should depend on:

A useful policy decision returns more than allow or deny:

{
  "decision": "require_confirmation",
  "reason": "external_visibility",
  "scope": {
    "tool": "publish_document",
    "resource_id": "doc_42",
    "version": 7
  },
  "expires_at": "2026-09-30T10:30:00Z"
}

Confirmation must bind to the exact action. A user approving "publish version 7 of document 42" should not authorize a later publish of version 8 or another document.

Separate preview from commit

High-impact tools benefit from a two-phase interface:

  1. Prepare validates inputs and returns a complete preview.
  2. Commit accepts a preview ID and a scoped confirmation token.

The preview should show the target, final content, recipients, monetary amount, and irreversible effects. The commit endpoint should reject stale previews.

4. Design idempotency before retries

Retries are only safe when repeated execution preserves the intended business result.

Use a business-scoped key

An idempotency key should identify the intent, not the network attempt:

task_id + tool_name + normalized_target + semantic_version

Generating a random key for every retry defeats idempotency. Reusing one key across unrelated actions can incorrectly collapse valid operations.

The downstream service should store:

If the same key arrives with different arguments, return a conflict instead of guessing.

Treat timeouts as unknown outcomes

A timeout means the caller did not observe a result. It does not prove that the server did nothing.

request sent
  → caller timeout
  → server may have committed
  → retry without reconciliation
  → duplicate side effect

After a timeout:

  1. query by idempotency key or business identifier;
  2. verify whether the desired object exists;
  3. continue if invariants match;
  4. compensate or escalate if state is partial;
  5. retry only if absence is authoritative.

5. Use typed failure semantics

The tool adapter should map service-specific errors into a small stable taxonomy.

Error type Retry? Typical action
TRANSIENT Yes, bounded Backoff and retry
RATE_LIMITED Yes, scheduled Respect retry-after
INVALID_INPUT No Correct arguments
UNAUTHORIZED No Request authorization
CONFLICT Usually no Re-read state and re-plan
PARTIAL_SUCCESS No blind retry Reconcile or compensate
UNKNOWN_OUTCOME Not yet Query authoritative state
POLICY_BLOCKED No Explain boundary or escalate

The raw downstream message can still be attached for operators, but the decision loop should act on the typed category.

Bound every retry policy

Define:

Retries without these limits can amplify outages and create tool-call storms.

6. Verify the post-action state

The authoritative state is often different from the tool response.

Action Verification source Example invariant
Create calendar event Calendar read API Time and attendee set match
Send message Message history One message with idempotency key exists
Update issue Issue read API State and assignee match
Submit workflow Workflow instance API Instance is accepted and linked
Delete object Object read or tombstone Object is absent or marked deleted

Verification should be:

result = create_event(request, idempotency_key)
event = get_event(result.event_id)

assert event.status == "confirmed"
assert event.start_at == request.start_at
assert required_attendees <= set(event.attendee_ids)

If verification fails, do not report success. Return a typed partial or unknown state.

7. Protect sensitive data

Tool arguments and results often contain the most sensitive information in an agent trace. Protection should begin at the schema:

Avoid generic recursive redaction as the primary control. It misses encoded data and can over-redact useful evidence. Field-level policies are more reliable.

Minimize model-visible output

The model rarely needs the entire API response. Tool adapters should return a bounded semantic result:

{
  "event_id": "evt_42",
  "status": "confirmed",
  "start_at": "2026-10-02T15:00:00+08:00",
  "attendee_count": 6
}

Keep raw payloads outside the prompt for debugging under stricter access controls.

8. Instrument the transaction

Every call should emit enough telemetry to answer:

A compact event schema:

{
  "task_id": "task_8f2a",
  "step_id": "step_06",
  "tool": "calendar.create_event",
  "tool_version": "3",
  "risk": "reversible_write",
  "policy_decision": "allow",
  "attempt": 1,
  "duration_ms": 184,
  "result_type": "success",
  "verification": "passed",
  "input_tokens": 0,
  "output_bytes": 418
}

Aggregate metrics should include tool selection accuracy, argument validity, authorization rejection rate, retry rate, unknown-outcome rate, verification failure rate, and P95 latency.

9. Evaluate tools independently from the full agent

Tool quality can be tested without a live model.

Contract tests

Execution tests

Model-facing tests

Evaluate selection, arguments, execution, and verification separately. A single "task passed" label cannot reveal which contract failed.

Release checklist

Contract

Authorization and effects

Execution

Verification

Privacy and observability

Conclusion

A reliable agent tool is not merely an API the model can call. It is a transaction protocol the harness can validate, authorize, execute, verify, observe, and recover.

When that protocol is explicit, tool use becomes testable engineering rather than optimistic automation.