Skip to content

ReasoningChain

ReasoningChain is the main public API: a list of steps plus execution settings. It runs them through the DAG executor and serializes to JSON for reuse.

ReasoningChain(steps, max_workers=3, ...)
ParameterTypeDefaultPurpose
stepsSequence[...]requiredThe steps to run (typed classes or legacy StepDescription).
max_workersint | str3Parallel worker pool size. "auto" sizes it for you.
fail_fastboolFalseStop scheduling new steps after the first final failure. See run controls.
timeoutfloat | NoneNoneChain-level timeout in seconds (active time, pauses excluded).
enable_progressboolFalseProgress logging.
search_configContextSearchConfig | NoneNoneContext extraction strategy (substring or vector).
metricslist[MetricBase] | NoneNoneChain-level metrics.
replan_policyReplanPolicy | NoneNoneRE-PLAN policy.
default_llm_configLLMStepConfig | NoneNoneDefault LLM config for LLM steps.
memory_schemadict | NoneNoneWrite-time memory validation schema.
chain_toolslist[ChainToolDefinition] | NoneNoneChains exposed as tools. See chain tools.
metadatadict | NoneNoneFree-form metadata stored on the chain.
prompt_templatePromptTemplate | NoneNoneCustom prompt template.
trace_name, session_idstr | NoneNoneTracing identifiers.
step_groupsSequence[StepGroup] | NoneNoneNamed step groups.
max_injectionsint50Upper bound on steps injected at run time.

The constructor validates the graph: duplicate numbers, unknown dependencies, cycles (except declared loops) and chain tool boundaries raise ValueError.

result = chain.execute(context) # synchronous
result = await chain.execute_async(context) # async
async for item in chain.stream_async(context):
... # each StepExecutionResult as it finishes, then the ReasoningResult

execute and execute_async return a ReasoningResult: success, get_final_output(), step_results, token usage, metadata and an execution trace. See async execution and streaming.

A failed step does not raise; it gives success=False on its StepExecutionResult and on the chain result. Cancellation and the chain timeout do raise. See run controls.

report = chain.preflight(context)
if not report.all_present:
print(report.format_text())

preflight compares what the chain needs with what the context provides: tools, MCP servers, skills, code profiles, Claude Code CLIs and the Codex runtime. See preflight.

Other dry-run helpers:

  • chain.required_tools(): tool names the steps reference.
  • chain.estimate_cost(pricing=...): token/USD projection before running. Agent steps that run outside CARL (claude_code, codex) are not modelled.
  • chain.to_mermaid(): the DAG as a Mermaid diagram.
chain.save("my_chain.json")
loaded = ReasoningChain.load("my_chain.json")
d = chain.to_dict()
typed = ReasoningChain.from_dict_typed(d) # typed step classes
legacy = ReasoningChain.from_dict(d) # legacy StepDescription where possible
s = chain.to_json(); ReasoningChain.from_json(s)
  • Every dict carries format_version (15 in CARL 0.5.2) and carl_version. A newer format_version than the library knows raises ChainFormatNewerError. Older dicts are lifted with ReasoningChain.from_dict(ReasoningChain.migrate(raw)).
  • from_dict, from_json and load build legacy StepDescription objects for the classic step types; use from_dict_typed to get typed classes everywhere.
  • fail_fast, max_injections, memory_schema, step_groups, chain_tools, loops (loop_back_to, loop_config), triggered_by, and collaboration topology (sub_chain, agents, base_step) round-trip.
  • Not serialized: metrics and cache on steps, the prompt template, and all host authority (tools, policies, providers, enforcers, allow_local_agent_execution, decision_endpoints). Re-attach them after loading.

Details: JSON format and migration.

  • chain.reflect(...): analyse a completed run.