LLM & AI agents

AI Agent Design: Tool Calling, State and Workflows

The article distinguishes decisions that benefit from an agent from steps better handled by a deterministic workflow. Narrow tool schemas, state transitions, timeouts and idempotency explain how to avoid accidentally repeating an operation. A refund example makes approval binding concrete: approval must cover the exact transaction, and success must be tested against real system effects rather than the final response alone.

On this page

Agent versus deterministic workflow

A deterministic workflow specifies which step follows each condition. An agent lets a model choose some next actions. This flexibility broadens the testing surface. If an order ID is already known and one API call solves the task, a full agent loop may be unnecessary. Agents become useful when intent is ambiguous and multiple tools or information sources need coordination. The first design question should be which decisions require probabilistic reasoning. Business rules and authorization remain in deterministic application logic.

Define tool contracts explicitly

Each tool needs a narrow purpose and a stable name, input schema, error vocabulary and output contract. Prefer scoped operations such as “get order status” and “create refund request” over a tool that can arbitrarily modify the database. Validate types, ranges and IDs against the caller's authorization context. The model may construct invalid arguments; the server must reject them before execution. Tool results are external data, not trusted new instructions to the agent.

Execution flow for a tool-using agentUser intentPlanning choiceTool callPolicy + executionVerified result
Figure 1. Tool selection is bounded by argument validation and state transitions; state-changing operations wait for approval when required.

How is state maintained in an agent loop?

Agent state extends beyond conversation history: which tool ran, which record was affected and which approval is pending? Those details determine whether resuming interrupted work repeats an earlier step. Idempotency keys help prevent network uncertainty from becoming duplicate side effects. User-facing explanations and application state therefore remain separate. A response that looks complete on screen can still have an unfinished operation behind it.

Handling failures, timeouts and retries

A timeout can mean that an operation completed but its result never reached the client. Reading an order status again therefore carries a different risk from sending the same refund again. Looking up transaction state and an idempotency record separates an uncertain write from a blind retry. Bounded attempt counts and total duration also prevent an endless tool-call loop. The user-facing error still needs to be understandable without exposing internal system details or secrets.

Where should human approval enter the workflow?

Approvals for payments, deletions, significant purchases or outbound communications should be enforced outside the model. Show the proposed target, amount, impact and reversibility to the reviewer. The executed arguments must match what was approved; stale data may require revalidation. Link approval records to user identity and operation version. A model-generated sentence claiming that approval occurred is not an approval event. Lower-risk actions may be automated more aggressively, but only through explicit risk policy.

Distinctions that matter in designModel proposesSchema constrains argsServer enforces policyRecord verifies result
Figure 2. Producing a plan, calling a tool and successfully completing an operation are not the same event.

Why are tool outputs treated as untrusted data?

A retrieved web page, PDF or ERP note can contain instructions such as “ignore all previous rules.” Those instructions are data, not authorized user or system commands. Keep tool outputs within an untrusted-data boundary and prevent them from elevating privileges. Directly converting arbitrary HTML or message content into SQL, shell commands or privileged action arguments is risky. Authorization and argument validation must live in the tool implementation. A warning in the prompt is not a substitute for enforcement.

How do you evaluate an agent?

An agent with a correct final answer may have created the wrong record or made unnecessary tool calls along the way. Task completion, tool choice, arguments, side effects and handoffs are distinct outcomes. Mock tools and bounded test environments expose errors and delays without changing real payments or production records. Traces with sensitive fields masked connect model decisions to actual API calls; conversation text alone cannot establish that connection.

What scope makes a good first agent pilot?

Looking up a record, finding a policy and drafting a response form an understandable, bounded agent application. That scope makes model decisions and fixed business rules easier to distinguish. Adding writes introduces approval and idempotency responsibilities. Releasing many tasks at once makes failures harder to isolate. If a deterministic workflow solves the same job more clearly, not using an agent can be a valid design outcome.

A refund operation that times out

Illustrative scenario; not a measured project result.

Consider an assistant handling a refund request. It may find an order, retrieve the refund policy and prepare a checklist. But issuing a payment reversal requires authorization and an explicit approval path. If a network timeout triggers a retry, the refund must not be executed twice. This scenario illustrates agent design and is not a claim about a tested payment integration.

The response is complete; is the operation complete?

In the refund example, a clear explanation and a payment applied once to the correct record are separate events. Transaction identifiers, stored state and approval bound to exact arguments connect them. Much of agent design is therefore about which operation the application executes under which conditions, rather than what the model says. A fixed workflow that already meets those conditions does not automatically need a freer agent loop.

Frequently asked questions

Are tool calling and agents the same?

No. Tool calling is an interface; agents add step selection and workflow state.

What happens if an agent executes an action twice?

Side-effecting tools should use idempotency keys and operation IDs.

Is prompt-based approval sufficient?

No. Approval must be an independently verified application event.

References and further reading

  1. OpenAI — Agents SDK
  2. OpenAI — Guardrails and Human Review
  3. OWASP — Top 10 for LLM Applications
  4. OpenAI — Integrations and Observability