Nous.Agents.ReActAgent (nous v0.13.3)

View Source

ReAct (Reasoning and Acting) Agent behaviour implementation.

ReAct is a prompting paradigm where AI agents interleave:

  • Reasoning: Thinking about what to do next
  • Acting: Using tools to gather information or perform actions
  • Observing: Processing results to inform the next step

This module implements Nous.Agent.Behaviour with enhanced capabilities:

  • Structured planning with facts survey
  • Built-in todo list management
  • Note-taking for observations
  • Loop prevention (warns on duplicate tool calls)
  • Mandatory final_answer for task completion

Built-in Tools

  • plan - Create structured plan before taking action
  • note - Record observations and insights
  • add_todo - Track subtasks
  • complete_todo - Mark tasks done
  • list_todos - View current todos
  • final_answer - Complete the task (required)

Example

agent = Agent.new("openai:gpt-4",
  behaviour_module: Nous.Agents.ReActAgent,
  tools: [&search/2, &calculate/2]
)

{:ok, result} = Agent.run(agent,
  "Find the oldest F1 driver and when they won their first championship"
)

Based on Research

This implementation draws from:

  • "ReAct: Synergizing Reasoning and Acting in Language Models" (Yao et al., 2023)
  • HuggingFace smolagents toolcalling_agent patterns

Summary

Functions

Track tool calls for loop detection.

Build messages with ReAct system prompt.

Extract output from final_answer or last assistant message.

Get all tools including ReAct-specific tools.

Initialize context for ReAct execution.

Process response and check for final_answer.

Functions

after_tool(agent, call, result, ctx)

Track tool calls for loop detection.

build_messages(agent, ctx)

Build messages with ReAct system prompt.

Combines the ReAct system prompt with any user instructions.

extract_output(agent, ctx)

Extract output from final_answer or last assistant message.

When output_type is set, parses and validates the answer text.

get_tools(agent)

Get all tools including ReAct-specific tools.

init_context(agent, ctx)

Initialize context for ReAct execution.

Sets up ReAct-specific state:

  • todos - Task list
  • plans - Planning history
  • notes - Observations
  • tool_history - Loop detection
  • final_answer - Captured final answer

process_response(agent, response, ctx)

Process response and check for final_answer.

Updates context and sets needs_response to false when final_answer tool is called.