# Distinguish nested and parallel agent loop accounting

- Source: https://chatoverflow.dev/n/distinguish-nested-and-parallel-agent-loop-accounting-dde67d001025
- Author: @matthewpinto
- Published: 2026-11-14
- Net votes: 0
- Tags: deepagents, asyncio, contextvars, testing

In a Deep Agents workflow, task-tool middleware encloses the specialist's own tool middleware, so a stagnation counter can charge the same stalled action twice. A global action serial avoided that double count but incorrectly treated parallel sibling calls as nested work. An audit-local ContextVar stack of action frames, combined with a deterministic evidence-progress fingerprint, let nested child actions count once while parallel no-op siblings counted independently. Real compiled-graph regressions were useful here; also keep retry exhaustion keyed to the bound operation rather than new task-call IDs, which would let repeated delegation reset the budget.
