Skip to content

Lesson 2 of 9 · 27m

Tool Contracts That Survive Models

Design tools as narrow, typed, idempotent APIs with useful errors instead of exposing broad internal functions to the model.

Most agent demos wire the model directly to whatever functions were nearby: search, read_file, query_database, run_sql. That is how you get a confident assistant issuing DELETE FROM users because the schema looked interesting. Tools are the agent's actuators, and actuator design is where safety becomes concrete.

Narrow beats powerful

A good tool does one business operation and owns its validation. Compare these interfaces:

# Bad: unlimited capability, no business semantics
run_sql(query: str) -> list[dict]

# Better: constrained, reviewable, meaningful
get_customer_invoice(customer_id: str, invoice_id: str) -> Invoice
refund_invoice(invoice_id: str, amount_cents: int, reason: str) -> RefundResult

The narrow version lets you enforce tenancy, limits, confirmation, audit logging, and idempotency inside the tool. The broad version forces the model to reconstruct your entire authorization design every time.

Schemas are a contract, not documentation garnish

Use typed schemas with strict validation. Reject unknown arguments and make dangerous units explicit:

{
  "name": "refund_invoice",
  "parameters": {
    "type": "object",
    "additionalProperties": false,
    "required": ["invoice_id", "amount_cents", "reason"],
    "properties": {
      "invoice_id": {"type": "string", "pattern": "^inv_[a-z0-9]{24}$"},
      "amount_cents": {"type": "integer", "minimum": 1, "maximum": 500000},
      "reason": {"type": "string", "maxLength": 500}
    }
  }
}

Never make the model guess whether an amount is dollars or cents, whether an identifier is internal or public, or whether null means delete. Those design shortcuts become incident reports.

Errors should teach recovery

Tool errors are feedback for the control loop. Return structured failures the agent can reason about:

{
  "ok": false,
  "error": {
    "code": "invoice_already_refunded",
    "retryable": false,
    "message": "Invoice inv_8f2... already has refund re_19c...",
    "safe_next_actions": ["get_invoice", "stop"]
  }
}

Exception: something went wrong invites repeated attempts. A precise error tells the agent whether to correct arguments, ask for confirmation, or stop.

Idempotency is non-negotiable

Agents retry. Networks retry. Humans refresh. Any mutating tool needs idempotency keys or natural unique constraints:

create_ticket(
    idempotency_key=run_id + ":create-ticket",
    title=title,
    body=body,
)

Without idempotency, your retry policy is a duplicate-write generator.

Hide raw power behind composition

Sometimes you genuinely need a flexible primitive such as shell execution or browser control. Keep it behind a higher-level tool when possible, and when not possible, isolate it in a sandbox with explicit filesystem, network, and credential boundaries. The model should receive the least powerful interface that can accomplish the job.

Field notes

  • Tool names are prompts. delete_user will be used more casually than deactivate_user_account even when both are dangerous.
  • Return compact results. A 300KB response consumes context, truncates badly, and hides the one fact the agent needed.
  • Do not expose staging-only cleanup tools in production because they are convenient in development.
  • Test tools independently of the model. If the tool is unsafe with random arguments, no prompt will make it safe.

Was this lesson helpful?

Sign in to rate