Practical technical guides / Agent-era architecture: capability boundaries

Expose Only the Tools the Current Workflow State Allows

Derive the agent tool list from persisted workflow state on every turn and re-check it at execution, so a refund tool cannot run before verification.

By Sandeep Belgavi · Published 2026-11-26 · Prepared 2026-11-25

Derive the agent tool list from persisted workflow state on every turn and re-check it at execution, so a refund tool cannot run before verification.

One flat tool list is an authority decision nobody made

Most first agent integrations register every tool the application has and rely on the system prompt to explain when each may be used. A help-desk agent for Belgavi's Book Store might receive lookup_order, request_receipt_photo, mark_verified, issue_refund and close_case on every turn, along with the sentence "only refund after the receipt is verified". That sentence is a request to the model. The tool list is a capability grant. When the two disagree, the grant wins, because a model that emits a well-formed issue_refund call will have it executed by any runtime that only checks the schema.

The established pattern that fits here is older than agents: a workflow state machine whose states decide which operations are legal. What changes with an agent is where the check must live. A deterministic workflow usually checks legality once, inside the command handler. An agent workflow needs the check twice: once when building the turn, so the model only sees tools that are legal in the current state, and again when a proposed call arrives, because the state may have changed between those two moments.

This guide works through that design on a synthetic refund case, CASE-4107, and then covers migration from a flat list, the failure modes that remain, and the situations where a state machine without any agent is the better answer.

Make the allowed set a function of persisted state

Start by writing the states down and attaching a closed allowlist to each one. For the refund flow the table is short.

# Author: Sandeep Belgavi. State-scoped tool allowlist for a synthetic refund case.
ALLOWED = {
    "awaiting_verification": {"lookup_order", "request_receipt_photo", "mark_verified"},
    "verified": {"lookup_order", "issue_refund"},
    "refunded": {"lookup_order", "close_case"},
    "closed": set(),
}
TRANSITIONS = {"mark_verified": "verified", "issue_refund": "refunded", "close_case": "closed"}

def visible_tools(case):
    if case.state not in ALLOWED:
        raise ValueError(f"unknown state {case.state!r}")
    return sorted(ALLOWED[case.state])

Three properties matter more than the code. First, the allowlist is closed: a tool absent from the table is not available in any state, and an unknown state raises instead of falling back to "all tools". Second, visible_tools reads only persisted state, never the conversation transcript. If the transcript says the receipt was verified but the case row still says awaiting_verification, the refund tool stays hidden. Third, the transition table is separate from the allowlist, so a review can ask two distinct questions: which tools may run here, and where does each successful call lead.

Notice that lookup_order appears in three states. Read-only tools are often safe to expose broadly; the allowlist is where that judgement becomes explicit and reviewable rather than implied by a prompt.

Check again when the call arrives

Hiding a tool is not enough. The model can still name a tool that was legal on an earlier turn, a cached plan can replay an old call, and two workers can act on the same case. The executor therefore repeats the check against the current state and, for effects, against the revision the turn was built from.

def execute(case, tool, listed_revision):
    if tool not in TOOLS:
        return "rejected_unknown_tool"
    if tool not in ALLOWED.get(case.state, set()):
        return "rejected_not_allowed_in_state"
    if TOOLS[tool]["effect"] in {"write", "money"} and listed_revision != case.revision:
        return "rejected_stale_listing"
    nxt = TRANSITIONS.get(tool)
    if nxt:
        case.state, case.revision = nxt, case.revision + 1
    return "applied"

In a database the revision comparison belongs in the update predicate, so the check and the transition are one atomic statement and an affected-row count of zero means another writer won. The dictionary version above shows the decision order, not durability.

Work through CASE-4107

The companion example runs this sequence and records the outcome of each call.

Turn one. The case is in awaiting_verification at revision 0. The model sees lookup_order, mark_verified and request_receipt_photo. Suppose it ignores the listing and proposes issue_refund anyway, perhaps because the customer, Geetha, insists that she already sent the receipt. The executor returns rejected_not_allowed_in_state and nothing changes. The agent then calls mark_verified, which is legal, and the case moves to verified at revision 1.

Turn two. A new listing is built from revision 1 and now includes issue_refund. Before this worker's call arrives, a second worker processing a duplicate message issues the refund first, moving the case to refunded at revision 2. When the first worker's issue_refund arrives, the state check rejects it. If the state had somehow still been verified, the revision check would have rejected it as a stale listing. Either way there is exactly one refund.

Turn three. The listing for revision 2 offers only lookup_order and close_case.

The example also tests the opposite direction: a read-only lookup with a stale revision is allowed, because reads do not need the same protection. That asymmetry is a design choice; if reads expose sensitive data whose visibility depends on state, treat them like writes.

What stays deterministic and what the agent still decides

The state machine owns which states exist, which tools each state permits, what each successful call transitions to, and the concurrency check. Those rules are testable without a model, and the companion test suite enumerates them in under a millisecond.

The agent keeps the work that benefits from language understanding: reading the customer's message, deciding whether the attached photo looks like a receipt worth sending to verification, choosing among the legal tools, and writing the reply. It does not decide whether a refund is legal at this point in the process. That boundary is the whole point of the pattern, and it is visible in the code rather than only in the prompt.

Approval steps fit the same table. If refunds above a threshold need a person, add an awaiting_approval state whose allowlist contains no money tools, and let the approval itself be the transition. The model can draft the approval request but cannot satisfy it.

Migrating from a flat tool list

You rarely start greenfield. A practical migration from a flat list proceeds in observable steps.

First, log every tool call with the case state at execution time, without blocking anything. After a week of traffic you have a matrix of state against tool and can see which combinations actually occur. Combinations that never occur are candidates for removal from the allowlist; combinations that occur but should not are bugs the prompt was hiding.

Second, enforce at the executor while still listing every tool. Disallowed calls now fail with a typed error the agent can read and recover from. This is the step that changes safety, so roll it out per state, starting with states that permit money or irreversible writes.

Third, scope the listing. Once enforcement is stable, build each turn's tool list from the allowlist. This mostly improves quality and cost rather than safety: fewer tool definitions mean fewer tokens and fewer wrong choices.

Fourth, move state out of the prompt. Phrases like "only refund after verification" can be deleted once the table enforces them. Keeping them is harmless, but they should no longer be the control.

Failure modes that remain

The state itself can be wrong. If mark_verified accepts any photo, the allowlist faithfully permits a refund on a bad verification. State scoping moves the question to the transition that enters each state, which is where the review effort should go.

Tool names are not capabilities. If issue_refund accepts an arbitrary amount, scoping the tool by state does not bound the amount. Argument limits belong in the tool's own validation, checked against the case record, not against model output.

Caching tool lists across turns reintroduces the flat-list problem. Providers and frameworks that let you cache the tool section of a request make it tempting to keep one list for the whole conversation. If you cache, cache per state, and keep the executor check regardless.

Parallel tool calls need the revision check applied per call. Two calls emitted in one model response were built from the same listing; the first may change state and invalidate the second.

When you do not need an agent here

If every transition in the table is triggered by a structured event such as a form submission, a payment callback or a timer, there is nothing for a model to interpret, and the state machine alone is the system. Add an agent where input is unstructured, such as free-text customer messages, photos or ambiguous order histories, and keep it inside the states where that interpretation helps.

Verification checklist

Assert that every tool named in the allowlist exists and every transition target is a known state. Assert that each state's listing equals its allowlist exactly. For each state, attempt every tool not in its allowlist and expect a typed rejection with no state change. Replay a call with an old revision and expect rejected_stale_listing for effects. Run two workers against the same case and assert exactly one money effect. These tests are deterministic, so they belong in the ordinary unit suite, not an evaluation run. For a complementary check that the guard rules themselves have no gaps or overlaps, see the guard coverage matrix guide at /blogs/system_design/agent_guard_coverage_matrix.html.

Primary references and authorship

Sources checked 2026-11-25. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/state_scoped_tools. Views are my own and do not represent any employer, past or present.