Skip to content

The graph: runs, steps and turns

The previous pages made individual calls work. This one makes them one task. An agent that drives its own loop makes many gateway calls to do one job, and the gateway cannot know which belong together unless the agent says so. If it does not, one application lands in the run ledger as eight one-action “runs”, and every signal built on runs (cost per processed outcome, run length, drift baselines, replay) is computed over fragments.

The ledger counts four things, nested (Runs, steps, turns & actions):

Unit In the screening agent Header
Run One application, start to recorded recommendation x-brutor-run-id
Step A phase of the task: gather, assess, decide X-Brutor-Step-Id, X-Brutor-Step-Name
Turn A model pass, with the calls it feeds or triggers: t1, t2 X-Brutor-Turn-Id, X-Brutor-Turn-Seq
Action One governed call (each request)

Steps are phases of the task, not graph nodes. Naming steps after nodes would make a refactor of the graph look like a change in behaviour.

screening_agent/graph.py
NODES = ["intake", "verify_identity", "credit_report", "affordability",
"classify", "fraud_screen", "decide", "record"]
STEP_NAMES = {
"gather": "Gather the application facts",
"assess": "Assess risk and fraud",
"decide": "Decide and record",
}
TURNS = {"t1": ("t1", 1), "t2": ("t2", 2)}
# node -> (step id, turn id)
NODE_MAP = {
"intake": ("gather", "t1"),
"verify_identity": ("gather", "t1"),
"credit_report": ("gather", "t1"),
"affordability": ("gather", "t1"),
"classify": ("assess", "t1"), # the classifier call the four reads feed
"fraud_screen": ("assess", "t2"),
"decide": ("decide", "t2"),
"record": ("decide", "t2"), # the last action carries X-Brutor-Run-End
}
def node_headers(node: str) -> dict:
step, turn = NODE_MAP[node]
return {"step_id": step, "step_name": STEP_NAMES[step], "turn": TURNS[turn], "node": node}

Every node passes **hdr("<node>") to its gateway call, as you saw on the MCP, skill and A2A pages. The node name itself is not a header; it appears as node= in the agent’s log line.

The graph is compiled linearly. There is no conditional edge and no loop, which is why a run of this agent can never end exhausted:

screening_agent/graph.py
graph = StateGraph(ScreeningState)
previous = START
for node in NODES:
graph.add_node(node, nodes[node])
graph.add_edge(previous, node)
previous = node
graph.add_edge(previous, END)
return graph.compile()

The decision logic between the nodes is deterministic and lives in rules.py: a policy floor computed from the identity check, the affordability class, the fraud verdict and the risk band, given to the drafter before it writes (so the letter matches the outcome), and applied again after it, overriding the model. The rationale says which rule fired.

process_application opens the run, invokes the graph, and makes sure the run is closed whatever happens:

screening_agent/graph.py
def process_application(gw, settings, store, application_id, *, graph=None) -> RunResult:
compiled = graph or build_graph(gw, settings, store)
run_id = f"bds-{application_id}-{new_ulid()}" # ≤ 128 chars, unique per attempt
with gw.run(run_id) as ctx:
try:
final_state = compiled.invoke({"application_id": application_id})
outcome = final_state["outcome_id"]
result = RunResult(application_id, run_id, "completed", outcome, ...)
if not ctx.closed:
gw.end_run("completed", outcome) # the header close did not take
except PolicyBlocked as exc:
gw.end_run("errored") # cause: blocked_by_policy
result = RunResult(..., "errored", None, failure=classify_failure(exc),
blocked_by_policy=True)
except Exception as exc:
gw.end_run("errored")
result = RunResult(..., "errored", None, failure=classify_failure(exc))
return result

The run id is the agent’s, and a new one per attempt: a retried application is a second run, not a continuation of the first. The trace id (traceparent) is per run too, with a new span id per call, so OpenTelemetry tooling sees the same shape the ledger does.

What one application’s run looks like on the wire:

# Node Step Turn (seq) Run end
1 intake gather t1 (1)
2 verify_identity gather t1 (1)
3 credit_report gather t1 (1)
4 affordability gather t1 (1)
5 classify assess t1 (1)
6 fraud_screen assess t2 (2)
screener’s model call, depth 1 none its own turn
7 decide decide t2 (2)
8 record decide t2 (2) completed + outcome

A completed run reads 3 steps, 3 turns and 9 actions; 10 when held for an underwriter, because of the extra note. Those counts are what the run-length baseline learns, so a change in them is a drift signal with a cause.

One run overlaid on the Composition graph: nine actions in sequence, grouped by the steps the agent declared, with the Fraud Screener’s model call joining as action 6a.

Every ten minutes the scheduler polls for work. That poll is a governed call too (applications_list_pending), and a call with no run headers makes the gateway mint a run of its own that the idle sweeper later closes as abandoned, skewing the completion and abandonment rates the envelope grades. So the tick is a run of its own, bds-tick-<ulid>, with a single step poll_pending:

screening_agent/scheduler.py
out = self.gw.mcp_call(
self.settings.applications_mcp_server_id,
"applications_list_pending",
args, # {"limit": 50, "after": ...}
step_id="poll_pending",
step_name="Tick: poll pending applications",
run_end="completed" if outcome_id else None, # closes the tick run on the last page
outcome_id=outcome_id, # no_applications_available | applications_dispatched
)

It closes with one of two no-work outcomes, no_applications_available or applications_dispatched, before any application is screened. Each application is then a run of its own. No-work outcomes do not count toward the baseline, so an empty poll is never read as a success of the work and does not dilute cost per processed outcome (next page).

The other housekeeping runs (applying an approved decision, re-raising a lapsed hold, handing an application off) are single-step, single-turn, single-action runs with self-describing step ids: apply_approved_decision, reraise_approval, give_up, approval_closed.

The headers’ grammar, limits and what a bad value does are in the Agent header reference; how the gateway derives and sweeps runs is in Run lifecycle.

Next: declare and report how runs end.