Skip to content

Async Execution

CARL is async at its core. A chain runs its steps through a DAG executor that runs in parallel whatever the dependencies allow.

result = chain.execute(context) # synchronous wrapper
result = await chain.execute_async(context) # inside async code

execute() wraps execute_async(). Called from inside a running event loop (a notebook, an async test, a web handler) it warns and falls back to a thread pool with a nested loop, which can deadlock on loop-bound clients. Use await chain.execute_async(...) there, or pass strict_async=True to make execute() raise instead of warn.

The executor groups steps into batches: steps whose dependencies are done run together; later batches wait only for what they depend on. max_workers caps concurrency within a batch (default 3; "auto" = batch size).

chain = ReasoningChain(steps=steps, max_workers=4) # or "auto"
LLMStepDescription(number=1, title="Revenue", aim="...", dependencies=[])
LLMStepDescription(number=2, title="Costs", aim="...", dependencies=[]) # runs with 1
LLMStepDescription(number=3, title="Profit", aim="...", dependencies=[1, 2]) # waits for both

chain.get_execution_plan() shows the batches before you run.

Each step in a batch runs on its own copy of the context. Memory is copy-on-write: siblings don’t see each other’s writes; later batches do. After the batch, each step’s written and deleted keys are merged back in step declaration order, so the step declared later wins on a same-key conflict. Each successful step’s history entry and newly registered tools are merged too. Registered tools are shared by reference and must be safe to call concurrently. See copy-on-write.

runtime_input is copied into every step copy. The event bus and the pause and cancel tokens are shared by reference, so one call on the original context reaches all running steps, and a wait step can see an event that a sibling emits in the same batch.

Set callbacks on the ReasoningContext:

context = ReasoningContext(
outer_context=data,
api=client,
on_step_start=lambda num, title: print(f"start {num}: {title}"),
on_step_complete=lambda r: print(f"done {r.step_number}: {r.success}"),
on_progress=lambda done, total: print(f"{done}/{total}"),
on_llm_chunk=lambda chunk: print(chunk, end=""), # token streaming
)
CallbackSignature
on_step_start(step_number, step_title)
on_step_complete(StepExecutionResult)
on_chain_complete(ReasoningResult)
on_progress(completed, total)
on_llm_chunk(chunk) or (chunk, *, step_number, stage) — CARL inspects the signature.
on_step_event(step_number, event_type, payload) — fine-grained events such as debate.turn_argument, parallel_sampling.sample, supervisor.route_selected. Exceptions inside it are logged and swallowed.
on_human_input_requestedasync (HumanInputRequest) -> HumanInputResponse — the provider for human-input steps.
on_command_approval_requested(CommandApprovalRequest) -> bool — approval for command steps; without it, commands that need approval are denied.
  • Chain: ReasoningChain(..., timeout=60.0). Counts active time only; idle time in an acknowledged pause is excluded.
  • Step: LLMStepDescription(..., timeout=30.0) or LLMStepConfig.timeout; tool steps use ToolStepConfig.timeout (default 30 s); other step types have their own bounds in their configs.
ControlCallEffect
Fail-fastReasoningChain(fail_fast=True)After the first final failure no new step starts; the rest are skipped.
Cancelcontext.cancel()Running steps stop cooperatively; unstarted steps are skipped with error_message="cancelled by user". Wakes a paused run.
Pausecontext.request_pause()No queued step starts; running steps finish. is_paused() turns True once nothing is active.
Resumecontext.clear_pause()Continues without re-running completed steps.

Details, including result.metadata["fail_fast"] and the difference between is_pause_requested() and is_paused(): run controls.

ReasoningContext.snapshot() captures history, memory, metadata and the cancel state as a ContextSnapshot (framework-internal __ metadata keys are dropped). Later:

result = await chain.execute_async(new_context, resume_from=snapshot)

Steps completed in the snapshot are skipped. Host authority objects (CommandPolicy, NetworkEnforcer, …) are not in the snapshot; the new context must get them from the host again.