ReActWorkflow¶
Built-in implementation of the ReAct (Reasoning and Acting) pattern.
Quick Example¶
from mamba_agents import Agent
from mamba_agents.workflows import ReActWorkflow, ReActConfig, ReActHooks
from mamba_agents.tools import read_file, run_bash
agent = Agent("gpt-4o", tools=[read_file, run_bash])
config = ReActConfig(
max_iterations=15,
expose_reasoning=True,
auto_compact_in_workflow=True,
)
hooks = ReActHooks(
on_thought=lambda s, t: print(f"Thought: {t}"),
on_action=lambda s, tool, args: print(f"Action: {tool}"),
)
workflow = ReActWorkflow(agent=agent, config=config, hooks=hooks)
result = await workflow.run("Analyze the codebase")
# Access scratchpad
for entry in result.state.context.scratchpad:
print(f"[{entry.entry_type}] {entry.content}")
Classes¶
ReActWorkflow¶
The main workflow class.
ReActConfig¶
Configuration extending WorkflowConfig.
ReActState¶
State tracking for ReAct execution.
ReActHooks¶
Callbacks extending WorkflowHooks.
API Reference¶
ReActWorkflow
¶
ReActWorkflow(
agent: Agent[DepsT, Any],
config: ReActConfig | None = None,
hooks: ReActHooks | None = None,
prompt_manager: PromptManager | None = None,
)
Bases: Workflow[DepsT, str, ReActState]
ReAct (Reasoning + Acting) workflow implementation.
Implements the Thought -> Action -> Observation loop until the agent calls the final_answer tool to signal task completion.
The workflow: 1. Sends the task to the agent 2. Agent thinks and calls tools 3. Tool results become observations 4. Loop continues until final_answer is called or max_iterations reached
Example
from mamba_agents import Agent from mamba_agents.workflows.react import ReActWorkflow, ReActConfig
agent = Agent("gpt-4o", tools=[read_file, run_bash]) workflow = ReActWorkflow( ... agent=agent, ... config=ReActConfig(max_iterations=10), ... ) result = workflow.run_sync("Find the bug in main.py") print(result.output) # Final answer print(result.state.context.scratchpad) # All steps
Initialize the ReAct workflow.
| PARAMETER | DESCRIPTION |
|---|---|
agent
|
Agent instance to use for execution.
TYPE:
|
config
|
ReAct-specific configuration.
TYPE:
|
hooks
|
Optional hooks for observability.
TYPE:
|
prompt_manager
|
Optional PromptManager for template resolution.
TYPE:
|
Source code in src/mamba_agents/workflows/react/workflow.py
prompt_manager
property
¶
prompt_manager: PromptManager | None
Get the prompt manager.
Returns None if no prompt manager was provided or created.
get_token_usage
¶
get_token_usage() -> TokenUsage
Get token usage statistics.
Delegates to the agent's usage tracker.
| RETURNS | DESCRIPTION |
|---|---|
TokenUsage
|
TokenUsage with total prompt/completion/total tokens. |
Source code in src/mamba_agents/workflows/react/workflow.py
get_cost
¶
Get estimated cost.
Delegates to the agent's cost estimator.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Estimated cost in USD. |
get_scratchpad
¶
Get the scratchpad entries from the last run.
| RETURNS | DESCRIPTION |
|---|---|
list
|
List of ScratchpadEntry objects, or empty list if no run yet. |
Source code in src/mamba_agents/workflows/react/workflow.py
get_reasoning_trace
¶
Get formatted reasoning trace from the last run.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted scratchpad text, or empty string if no run yet. |
Source code in src/mamba_agents/workflows/react/workflow.py
ReActConfig
¶
Bases: WorkflowConfig
Configuration for ReAct workflow execution.
Extends WorkflowConfig with ReAct-specific settings for reasoning trace visibility, termination strategy, and context management.
| ATTRIBUTE | DESCRIPTION |
|---|---|
expose_reasoning |
Whether Thought steps appear in workflow state.
TYPE:
|
reasoning_prefix |
Prefix for reasoning/thought in prompts.
TYPE:
|
action_prefix |
Prefix for actions in prompts.
TYPE:
|
observation_prefix |
Prefix for observations in prompts.
TYPE:
|
termination_strategy |
How to detect workflow completion ("tool" only for MVP).
TYPE:
|
final_answer_tool_name |
Name of the tool that signals completion.
TYPE:
|
auto_compact_in_workflow |
Whether to auto-compact context mid-workflow.
TYPE:
|
compact_threshold_ratio |
Ratio of context limit to trigger compaction.
TYPE:
|
max_consecutive_thoughts |
Max thoughts without action before forcing action.
TYPE:
|
include_scratchpad |
Whether to include full reasoning history in each iteration.
TYPE:
|
tool_retry_count |
Number of retries for failed tool calls.
TYPE:
|
system_prompt_template |
Optional template config for system prompt.
TYPE:
|
iteration_prompt_template |
Optional template config for iteration prompts.
TYPE:
|
Example
config = ReActConfig( ... max_iterations=15, ... expose_reasoning=True, ... termination_strategy="tool", ... )
With custom prompt templates¶
from mamba_agents.prompts import TemplateConfig config = ReActConfig( ... system_prompt_template=TemplateConfig(name="workflow/react_system"), ... iteration_prompt_template=TemplateConfig(name="workflow/react_iteration"), ... )
ReActState
dataclass
¶
ReActState(
task: str,
scratchpad: list[ScratchpadEntry] = list(),
current_thought: str | None = None,
current_action: str | None = None,
current_observation: str | None = None,
is_terminated: bool = False,
termination_reason: str | None = None,
final_answer: str | None = None,
iteration_token_counts: list[int] = list(),
total_tokens_used: int = 0,
compaction_count: int = 0,
consecutive_thought_count: int = 0,
)
Internal state for ReAct workflow execution.
This is stored in WorkflowState.context and tracks the full reasoning trace, termination status, and token usage.
| ATTRIBUTE | DESCRIPTION |
|---|---|
task |
The original user task/prompt.
TYPE:
|
scratchpad |
List of all Thought/Action/Observation entries.
TYPE:
|
current_thought |
The most recent thought generated.
TYPE:
|
current_action |
The most recent action taken.
TYPE:
|
current_observation |
The most recent observation received.
TYPE:
|
is_terminated |
Whether the workflow has terminated.
TYPE:
|
termination_reason |
Why the workflow terminated.
TYPE:
|
final_answer |
The final answer if workflow completed successfully.
TYPE:
|
iteration_token_counts |
Token count for each iteration.
TYPE:
|
total_tokens_used |
Total tokens used across all iterations.
TYPE:
|
compaction_count |
Number of times context was compacted.
TYPE:
|
consecutive_thought_count |
Consecutive thoughts without action (for forcing action).
TYPE:
|
add_thought
¶
Add a thought to the scratchpad.
| PARAMETER | DESCRIPTION |
|---|---|
thought
|
The reasoning/thought content.
TYPE:
|
token_count
|
Estimated token count.
TYPE:
|
Source code in src/mamba_agents/workflows/react/state.py
add_action
¶
Add an action to the scratchpad.
| PARAMETER | DESCRIPTION |
|---|---|
action
|
The action description (tool name and args).
TYPE:
|
token_count
|
Estimated token count.
TYPE:
|
metadata
|
Additional metadata (tool_name, tool_args, etc.).
TYPE:
|
Source code in src/mamba_agents/workflows/react/state.py
add_observation
¶
add_observation(
observation: str,
token_count: int = 0,
metadata: dict[str, Any] | None = None,
) -> None
Add an observation to the scratchpad.
| PARAMETER | DESCRIPTION |
|---|---|
observation
|
The tool result/observation content.
TYPE:
|
token_count
|
Estimated token count.
TYPE:
|
metadata
|
Additional metadata (tool_name, is_error, etc.).
TYPE:
|
Source code in src/mamba_agents/workflows/react/state.py
get_scratchpad_text
¶
get_scratchpad_text(config: ReActConfig) -> str
Format the scratchpad for prompt inclusion.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
ReActConfig containing prefix settings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted scratchpad text with prefixes. |
Source code in src/mamba_agents/workflows/react/state.py
get_thoughts
¶
get_actions
¶
get_observations
¶
ReActHooks
¶
ReActHooks(
on_workflow_start: Callable[..., Awaitable[None] | None]
| None = None,
on_workflow_complete: Callable[
..., Awaitable[None] | None
]
| None = None,
on_workflow_error: Callable[..., Awaitable[None] | None]
| None = None,
on_step_start: Callable[..., Awaitable[None] | None]
| None = None,
on_step_complete: Callable[..., Awaitable[None] | None]
| None = None,
on_step_error: Callable[..., Awaitable[None] | None]
| None = None,
on_iteration_start: Callable[
..., Awaitable[None] | None
]
| None = None,
on_iteration_complete: Callable[
..., Awaitable[None] | None
]
| None = None,
on_thought: ThoughtHook | None = None,
on_action: ActionHook | None = None,
on_observation: ObservationHook | None = None,
on_compaction: CompactionHook | None = None,
)
Bases: WorkflowHooks['ReActState', str]
Extended hooks for ReAct workflow observability.
Inherits the 8 base workflow hooks and adds 4 ReAct-specific hooks: - on_thought: Called when a reasoning trace is generated. - on_action: Called when a tool is about to be executed. - on_observation: Called when a tool result is received. - on_compaction: Called when context is compacted.
All hooks can be sync or async functions.
Example
def log_thought(state: ReActState, thought: str) -> None: ... print(f"Thought: {thought}")
hooks = ReActHooks(on_thought=log_thought) workflow = ReActWorkflow(agent, hooks=hooks)
Initialize ReAct hooks.
| PARAMETER | DESCRIPTION |
|---|---|
on_workflow_start
|
Called when workflow begins.
TYPE:
|
on_workflow_complete
|
Called when workflow completes successfully.
TYPE:
|
on_workflow_error
|
Called when workflow fails.
TYPE:
|
on_step_start
|
Called before each step executes.
TYPE:
|
on_step_complete
|
Called after each step completes.
TYPE:
|
on_step_error
|
Called when a step fails.
TYPE:
|
on_iteration_start
|
Called at the start of each iteration.
TYPE:
|
on_iteration_complete
|
Called at the end of each iteration.
TYPE:
|
on_thought
|
Called when a thought/reasoning is generated.
TYPE:
|
on_action
|
Called when a tool is about to execute.
TYPE:
|
on_observation
|
Called when a tool result is received.
TYPE:
|
on_compaction
|
Called when context is compacted.
TYPE:
|
Source code in src/mamba_agents/workflows/react/hooks.py
trigger_thought
async
¶
trigger_thought(state: ReActState, thought: str) -> None
Trigger the on_thought hook.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current ReAct state.
TYPE:
|
thought
|
The generated thought/reasoning.
TYPE:
|
Source code in src/mamba_agents/workflows/react/hooks.py
trigger_action
async
¶
trigger_action(
state: ReActState,
tool_name: str,
tool_args: dict[str, Any],
) -> None
Trigger the on_action hook.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current ReAct state.
TYPE:
|
tool_name
|
Name of the tool being called.
TYPE:
|
tool_args
|
Arguments passed to the tool.
TYPE:
|
Source code in src/mamba_agents/workflows/react/hooks.py
trigger_observation
async
¶
trigger_observation(
state: ReActState,
observation: str,
is_error: bool = False,
) -> None
Trigger the on_observation hook.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current ReAct state.
TYPE:
|
observation
|
The tool result or error message.
TYPE:
|
is_error
|
Whether this observation is an error.
TYPE:
|
Source code in src/mamba_agents/workflows/react/hooks.py
trigger_compaction
async
¶
Trigger the on_compaction hook.
| PARAMETER | DESCRIPTION |
|---|---|
result
|
The compaction result with details.
TYPE:
|