Lesson 5 of 5 Building Agents and Tool Integrations

Designing Reliable Tools for Agents

This lesson covers tool design, validation and error handling, since an agent is only as reliable as its tools.

Learning outcomes

After completing this lesson, you will be able to:

  • Write standardised function definitions.
  • Validate arguments and handle errors in Lambda tools.
  • Guard tool use with hooks and idempotency.

Standardized function definitions

A model chooses and calls tools based only on their definitions, so poorly defined tools lead to wrong choices and invalid arguments. Tool definitions should therefore follow a standard across the organisation. Names use a verb-noun form, such as get_order_status or create_refund. Descriptions explain the tool's purpose, when to use it and when not to use it. JSON Schemas give types, formats, enumerations of allowed values, numeric ranges and required fields, so that invalid arguments are rejected before the tool runs.

Fewer, well-scoped tools are easier for a model to use than many overlapping ones, because similar descriptions make the choice ambiguous. Tools that only read data should be separate from tools that change it, so that permissions, rate limits and human approval can be stricter for the tools that write.

Spot the bug

A weak tool definition

Exam skills 2.1.6

Find the lines that make this tool definition hard for a model to use correctly.

Log in to try this activity Interactive activities are free with an account.

Validation and error handling in Lambda

The schema guides the model but does not guarantee valid input, so the tool implementation, often a Lambda function, validates every argument again: types, formats, ranges, that the order belongs to the authenticated customer, and business rules such as refund limits. It authorises the action with the caller's identity from session attributes, not from arguments the model provides.

Errors are returned as structured, informative messages ("order_id must be 4 to 8 digits; received 'abc'") so the model can correct itself or ask the user, rather than raw stack traces. Timeouts prevent slow downstream systems from stalling the agent, and idempotency keys ensure that a retried create_refund call does not refund twice.

def handler(event, context):
    params = {p["name"]: p["value"] for p in event.get("parameters", [])}
    order_id = params.get("order_id", "")
    if not order_id.isdigit() or not 4 <= len(order_id) <= 8:
        return error(event, "order_id must be 4 to 8 digits")
    customer = event["sessionAttributes"]["customerId"]        # identity from the session, not from the model
    if not orders.belongs_to(order_id, customer):
        return error(event, "Order not found for this customer")
    refund = payments.refund(order_id, idempotency_key=f"refund-{order_id}")
    return ok(event, {"refund_id": refund.id, "eta_days": refund.eta_days})

The following diagram compares a refund tool without and with an idempotency key, which prevents duplicate refunds from retried calls.

Preventing duplicate refunds from retried tool calls. Without an idempotency key, a timed-out call that later succeeded and the agent's retry both refunded the order. With a key derived from the order and refund request, a timeout shorter than the retry interval and a structured error, the payment system ignores the duplicate.
Idempotency makes retries safe for every write tool, not only refunds.
Select all that apply

A refund tool in Lambda

Exam skills 2.1.6 · 2.1.3

A Lambda function implements an issue_refund tool for an agent. Choose the practices it should follow.

Log in to try this activity Interactive activities are free with an account.

Custom behaviours and guarding tool use

Frameworks let developers add behaviour around tool calls. In Strands Agents, hooks run before or after each tool call to log it, enforce rate limits, require approval for write tools or cancel calls that violate policy. Tool results should be size-limited and summarised so they do not flood the context window. The following table summarises reliability practices.

PracticePrevents
Clear names, descriptions and schemasWrong tool selection and invalid arguments
Server-side validationActing on malformed or malicious arguments
Identity from session, not argumentsActing on other users' data
Structured error messagesAgent loops and unhelpful failures
Timeouts and idempotency keysStalled agents and duplicate actions
Hooks for approval and loggingUnreviewed sensitive actions; missing audit trail
Worked example

Duplicate refunds

An agent occasionally issues two refunds for one order. Logs show that a tool call timed out, the agent retried, and both calls eventually succeeded.

The refund tool needs an idempotency key derived from the order and refund request so the payment system ignores the duplicate, plus a timeout shorter than the agent's retry interval and a structured error that tells the model to check refund status before retrying.

Check yourself

Where should a tool get the customer ID that it acts on?

Show answer

From the session context, never from model-supplied arguments, which can be manipulated.

Exam Tips

Remember for the exam

  • Validate tool arguments in the implementation, not only in the schema.
  • Take identity from session context, never from model-supplied arguments.
  • Return structured errors the model can act on.
  • Idempotency keys prevent duplicate side effects on retries.