Practical technical guides / Agent-era architecture

Separate Workflow Success From Completion of Optional Agent Checks

Derive a workflow outcome from required work only, let optional agent checks add warnings, and keep one failed check from cancelling the work that matters.

By Sandeep Belgavi · Published 2026-11-21

Derive a workflow outcome from required work only, let optional agent checks add warnings, and keep one failed check from cancelling the work that matters.

Two kinds of work in one step

Adding agents to an existing workflow usually adds work of a new kind. The original step had required work: look up the order, compute the refund, write the record. Then someone adds a model that reviews the tone of the reply, another that suggests a related book, and a third that summarises the case for the next shift. These checks are useful, but none of them decides whether the customer gets the refund. They are optional. The workflow still has to report one outcome, and that outcome is now easy to get wrong in two opposite directions.

A step can report failure because an optional check failed, even though everything the customer depends on succeeded. Or it can report success while a required task is still pending, was skipped, or was cancelled as a side effect of something else. Both mistakes come from deriving the outcome from "did every task finish without error" instead of from what each task means.

The example in this guide is a refund case, CASE-6101, at the fictional Belgavi's Book Store. The step runs four tasks. order_lookup and refund_amount are required. tone_review and reading_suggestion are optional agent checks.

How a task group can cancel the work that matters

Python's asyncio.TaskGroup is a good structured-concurrency primitive, and its documented rule is exactly what you want for required work: the first time any task in the group fails with an exception other than CancelledError, the remaining tasks in the group are cancelled. If the order lookup fails, there is no point computing a refund.

The same rule is wrong for optional work. Put all four tasks in one group, let tone_review raise after 20 ms, and the group cancels order_lookup at 20 ms, before its 80 ms of work completes. It also cancels refund_amount and reading_suggestion. The companion example records these states for the naive group: both required tasks cancelled, tone_review failed, and an outcome of failed. A refund that would have succeeded was abandoned because a reviewer of tone had a bad moment.

asyncio.gather has the opposite default. With return_exceptions false, the first exception propagates to the caller immediately, but the other awaitables are not cancelled and keep running. The caller sees a failure while work continues in the background with nobody waiting for its result. With return_exceptions true, exceptions come back in the result list, which is safer, but you still have to decide what each one means.

Separate the two lanes

The fix is to treat optional work as a different lane with its own error and deadline handling, and to keep the required lane strict.

async def optional(name):
    try:
        await asyncio.wait_for(work(name), optional_deadline)
        return 'ok'
    except TimeoutError:
        return 'timed_out'
    except RuntimeError:
        return 'failed'

async with asyncio.TaskGroup() as tg:
    required = {n: tg.create_task(work(n)) for n in REQUIRED}
    extra = {n: tg.create_task(optional(n)) for n in OPTIONAL}

A failing required task still cancels everything in the group, which is correct. An optional task cannot raise into the group, because its wrapper converts every expected failure into a state. Its deadline is its own: in the example a slow reading_suggestion is cut off at 300 ms and becomes timed_out, while the required work completes normally. Note what the wrapper does not catch. A programming error such as TypeError should still propagate. Only failures you have decided are acceptable for this check become states.

Derive the outcome with a pure function

The second half of the design is to compute the outcome from recorded states, separately from running the tasks. A pure function is easy to test exhaustively and easy to audit. The companion example uses these states: ok, failed, cancelled, timed_out, pending and skipped, plus an optional map from a task name to an approval reference for intentional skips.

The rules are short:

  1. Every known task must have a recorded state, and no unknown task may appear. A missing state means the record is incomplete, which is an error, not a success.
  2. A required task that is pending makes the outcome running. The step has not finished, whatever else has happened.
  3. A required task in any state other than ok makes the outcome failed, unless it was skipped with a recorded approval, in which case it becomes a warning.
  4. An optional task that is still pending at the decision point is a problem. Either wait for it, or cancel it and record timed_out. Leaving it running after you have reported the outcome means its result, or its side effect, arrives after anyone is looking.
  5. An optional task that was skipped needs a recorded reason. Otherwise it is indistinguishable from a bug that stopped it from running.
  6. Any other non-ok optional state becomes a warning. If there are warnings and no problems, the outcome is succeeded_with_warnings. Otherwise it is succeeded or failed.

The measured results from the example: the separated run with tone_review failing gives succeeded_with_warnings with the single warning tone_review failed. The slow suggestion gives the same outcome with reading_suggestion timed_out. A skipped tone_review without a reason gives failed. The same skip with the approval reference APPROVAL-17 gives succeeded_with_warnings. A skipped order_lookup without approval gives failed.

Why three outcomes and not two

A boolean success flag forces every optional failure into one of two wrong answers. succeeded_with_warnings lets downstream systems treat the refund as done while a dashboard counts degraded checks. It also gives you a number to watch. If tone_review fails on a fifth of cases after a model upgrade, the refunds still flow, but the warning rate tells you the check has stopped doing its job. Without the third outcome, that signal disappears into either the success count or the incident queue.

Be strict about one thing: an outcome with warnings is not a weaker kind of success that callers may also treat as failure when they like. Document what each outcome means for each consumer. A notification service might send the reply only on succeeded and route succeeded_with_warnings to a person, because the tone check did not run. An accounting service treats both as paid.

What stays deterministic

  • Which tasks are required and which are optional is fixed in code or configuration, reviewed like any other contract. An agent never decides at run time that a required task can be skipped.
  • Each optional task's deadline is a number in configuration, not a model's estimate.
  • The outcome is derived by the pure function from recorded states. The function has no access to model output, only to states.
  • Approvals for skipping required work are references to records created by a person or by a deterministic policy, checked to exist before they count.

Migration steps for an existing workflow

  1. List every task the step runs today, and mark each one required or optional with its owner. Disagreements here are the real design work.
  2. Record a state for every task, including the ones that never started. Many existing systems log only failures, so a task that silently did not run is invisible.
  3. Move optional tasks out of any task group, transaction or retry loop they share with required work, and give each one a deadline and a wrapper that converts expected failures into states.
  4. Write the outcome function, with tests for every state of every task. For four tasks and six states this is a small table.
  5. Expose the warning list and the warning rate per optional check, and decide what each consumer does with succeeded_with_warnings before you ship it.
  6. Remove any code path that retries the whole step because an optional check failed. Retrying required work that already succeeded is how duplicate refunds happen, unless every required task is idempotent.

Failure modes to test

  • An optional check fails before a required task finishes. Assert that the required task completes and the outcome is succeeded_with_warnings.
  • An optional check never returns. Assert that it is cut off at its deadline, recorded as timed_out, and that no result from it is applied after the outcome is reported.
  • A required task fails. Assert that optional work is cancelled too and the outcome is failed.
  • The state record is missing a task, or contains a task the function does not know. Assert a validation error rather than a guess.
  • An optional task has a side effect, such as posting an internal note. If it is cancelled at its deadline, decide whether a partial side effect is acceptable, and make that side effect idempotent if it can be retried later.

When the agent is unnecessary

Some optional checks are cheaper and more reliable as rules. A reply that must not include an internal order reference, or must stay under a length limit, does not need a model to review it. Keep models for checks that need judgment about language, and keep the deterministic checks in the required lane if the reply must not go out without them. A check that is required for compliance is not optional just because a model performs it. In that case it belongs in the required lane, and its failure should fail the step.

Verification checklist

  • The naive single-group version is in the test suite as a regression: it must not come back in a refactor.
  • Every combination of required-task states produces the outcome the table says.
  • Optional-check deadlines are enforced in tests with a deliberately slow fake.
  • The warning rate per optional check is visible on a dashboard, with an alert when it rises.
  • Skips without approval references fail, both for required and optional work.

Run the example yourself

The naive and separated task groups with real asyncio, the outcome function and eight tests. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Primary references and authorship

Sources read 21 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: