Practical technical guides / Agent-era architecture: explicit workflows versus autonomous loops

Mine Agent Traces for the Paths Runs Actually Take Before Keeping the Loop

Count tool-call variants in recorded agent runs, find the case attributes that decide the path, compile those paths into a workflow and keep the agent for the rest.

By Sandeep Belgavi · Published 2026-11-27

Count tool-call variants in recorded agent runs, find the case attributes that decide the path, compile those paths into a workflow and keep the agent for the rest.

The question to ask after three months of agent runs

Many teams put an agent loop in front of a process before they know what the process is. The help desk of Belgavi's Book Store is a typical case: an agent reads a case, calls tools such as get_order, check_return_window and create_return_label, and replies. It works, and after three months there are thousands of recorded runs. Now the useful question is no longer whether the agent can handle cases. It is how many cases needed an agent at all.

If most runs follow a handful of tool sequences, and the sequence is decided by facts already on the case, those runs are a workflow that the agent rediscovers on every call. An explicit workflow for them is cheaper, faster, easier to test and easier to audit. The agent stays for the cases whose path genuinely varies. This guide shows how to find out from the traces you already have, with a runnable example on synthetic data, and how to migrate without losing what the agent does well.

What to record

You need, for each run, the case attributes the decision could depend on and the ordered list of tools the agent executed. If the agent is instrumented with the OpenTelemetry GenAI semantic conventions, the second part is already in the traces: tool executions are execute_tool spans with gen_ai.tool.name as a required attribute. The conventions are still marked as development status, so pin the version you parse.

Sort the tool spans of each run by start time and keep the case attributes from the run's root span or the case record:

{"case": "CASE-5007", "kind": "return", "days_since_delivery": 12, "order_total": 900,
 "tools": ["get_order", "check_return_window", "create_return_label"]}

Two cautions. Record attributes as the workflow would see them at the start of the run, not values the agent computed along the way; otherwise you will discover that the path is decided by something only the agent knew. And keep the attributes you would be allowed to branch on in deterministic code. A free-text summary is not a branchable attribute.

Normalize, then count variants

In process mining, a variant is a distinct sequence of activities. Counting variants of raw tool sequences overstates the variety, because retries show up as repeats: a run that called get_order twice because the first call timed out is the same path as one that called it once. The companion example trace_path_mining collapses immediate repeats before counting.

# Author: Sandeep Belgavi. Excerpt from trace_path_mining/example.py
def normalize(tools):
    """Collapse immediate repeats (retries of the same tool) so they do not split variants."""
    out = []
    for t in tools:
        if not out or out[-1] != t:
            out.append(t)
    return tuple(out)

On the example's 400 synthetic runs, there are 58 raw variants and 49 after normalization. The top four variants alone cover 78.2 percent of runs. That number is the first signal: a long tail of variants with a heavy head is the shape of a mostly fixed process with exceptions.

Find the attributes that decide the path

Coverage alone is not enough. Two runs with the same variant may have taken it for different reasons, and a variant that covers many runs is only compilable if you can tell in advance which runs will take it. So for each kind of case, search for the smallest set of attributes whose groups each have one dominant variant. The example tries no attributes first, then each attribute alone, then both, and accepts a set when every group's most common variant has at least 90 percent of that group's runs.

The recorded result:

  • Refund-status questions need no attribute: 127 runs, 96.1 percent follow get_order > get_refund.
  • Returns are decided by whether delivery was within 30 days: within, get_order > check_return_window > create_return_label; after, the same first two steps and then escalate_to_person.
  • Damage claims are decided by whether the order total is above 2,000: above, the path includes request_approval before create_replacement.
  • Cases of kind "other" have no deciding attribute: 54 runs produced 41 distinct variants.

Notice what the search found in the damaged-claim group: an approval step that appears only above a threshold. That is a business rule the agent was following because its instructions said so. In an explicit workflow it becomes a branch you can read, test and change without editing a prompt.

Compile the covered paths and replay

Groups that decide their path and carry at least 5 percent of traffic become a compiled workflow; the rest go to the agent. Before trusting it, replay the compiled workflow against the recorded runs. For each run it owns, compare the path the workflow would take with the path the agent took.

The example's compiled workflow owns 346 of the 400 runs, 86.5 percent. Of those, 336 match the recorded path exactly, 97.1 percent. The remaining 54 runs, the free-form cases, stay with the agent.

The ten mismatches are the most valuable output. In all of them the agent did something extra at the end, in this fixture a search_help_articles call. Before switching traffic, someone has to look at each mismatch class and decide whether the extra step mattered. If it was noise, the workflow is right. If it caught something real, such as a customer who also asked an unrelated question, the workflow needs a branch, or a hand-off rule that sends such cases to the agent.

What the migrated system looks like

The result is not "remove the agent". It is a deterministic router in front of two kinds of handlers:

  1. The router reads the case attributes and either selects a compiled path or hands the case to the agent. The routing rules are the attribute sets the mining found, written as ordinary code with tests.
  2. Each compiled path is an explicit state machine: the same tools, called in a fixed order, with retries handled by the workflow engine instead of by repeated tool calls.
  3. The agent handles the residual, with the same tools it had before.
  4. Every compiled path has an exit to the agent for cases it cannot finish, for example when check_return_window returns a status the workflow does not know.

What stays deterministic is the routing, the order of steps on compiled paths and every approval threshold. What stays with the model is reading free-form requests, deciding what to do with unusual cases and writing replies, where wording varies and a template would be poor.

Migrating safely

Do it one group at a time, largest first. The cut-over mechanics, a routing facade, shadow comparison and segment-by-segment switching, are the strangler-fig pattern, which has its own guide on this site; what the mining adds is the evidence for which segments can move and in what order.

  • Run the compiled path in shadow for one group: it computes its decision, the agent still acts, and you compare. Keep counting mismatches by class.
  • When the mismatch rate is low and every class is understood, let the workflow act for that group, and keep the agent's tools and prompt unchanged for everything else.
  • Re-run the mining monthly. Processes drift: a new policy can turn a fixed path into a variable one, and the residual can grow a new dominant variant that is worth compiling.
  • Keep the purity threshold visible. A group compiled at 90 percent purity means one in ten of its runs used to take another path; make sure the workflow's exit to the agent covers them.

When the agent is the right design

The mining can also tell you to leave things alone. If no attribute set reaches a useful purity, or the top variants cover only a small share of runs, the process really is variable, and an explicit workflow would be a tangle of branches that the next policy change breaks. The "other" group in the example is that case. It is also the wrong exercise when volume is low: compiling a path that runs ten times a month saves little and adds code to maintain.

Limits of this guide

The traces in the example are generated, so its numbers describe the fixture, not any real agent. The example searches only two attributes and treats only immediate repeats as retries; real processes need a richer feature set and a review of which attributes may legally drive automated decisions. The OpenTelemetry GenAI conventions cited are at development status and may change.

Primary references and authorship

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