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.
Constructor
Section titled “Constructor”ReasoningChain(steps, max_workers=3, ...)| Parameter | Type | Default | Purpose |
|---|---|---|---|
steps | Sequence[...] | required | The steps to run (typed classes or legacy StepDescription). |
max_workers | int | str | 3 | Parallel worker pool size. "auto" sizes it for you. |
fail_fast | bool | False | Stop scheduling new steps after the first final failure. See run controls. |
timeout | float | None | None | Chain-level timeout in seconds (active time, pauses excluded). |
enable_progress | bool | False | Progress logging. |
search_config | ContextSearchConfig | None | None | Context extraction strategy (substring or vector). |
metrics | list[MetricBase] | None | None | Chain-level metrics. |
replan_policy | ReplanPolicy | None | None | RE-PLAN policy. |
default_llm_config | LLMStepConfig | None | None | Default LLM config for LLM steps. |
memory_schema | dict | None | None | Write-time memory validation schema. |
chain_tools | list[ChainToolDefinition] | None | None | Chains exposed as tools. See chain tools. |
metadata | dict | None | None | Free-form metadata stored on the chain. |
prompt_template | PromptTemplate | None | None | Custom prompt template. |
trace_name, session_id | str | None | None | Tracing identifiers. |
step_groups | Sequence[StepGroup] | None | None | Named step groups. |
max_injections | int | 50 | Upper 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.
Running a chain
Section titled “Running a chain”result = chain.execute(context) # synchronousresult = await chain.execute_async(context) # asyncasync for item in chain.stream_async(context): ... # each StepExecutionResult as it finishes, then the ReasoningResultexecute 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.
Check before running
Section titled “Check before running”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.
Serialization
Section titled “Serialization”chain.save("my_chain.json")loaded = ReasoningChain.load("my_chain.json")
d = chain.to_dict()typed = ReasoningChain.from_dict_typed(d) # typed step classeslegacy = ReasoningChain.from_dict(d) # legacy StepDescription where possibles = chain.to_json(); ReasoningChain.from_json(s)- Every dict carries
format_version(15in CARL 0.5.2) andcarl_version. A newerformat_versionthan the library knows raisesChainFormatNewerError. Older dicts are lifted withReasoningChain.from_dict(ReasoningChain.migrate(raw)). from_dict,from_jsonandloadbuild legacyStepDescriptionobjects for the classic step types; usefrom_dict_typedto 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:
metricsandcacheon 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.
Other chain methods
Section titled “Other chain methods”chain.reflect(...): analyse a completed run.
Ways to build a chain
Section titled “Ways to build a chain”- Directly: pass a list of typed step descriptions (this page).
ChainBuilder: a fluent builder.ChainBuilder.from_description: generate a chain from natural language.