Table of Contents

Class ReActHandler

Namespace
LMKit.Agents.Planning
Assembly
LM-Kit.NET.dll

Planning handler that implements the ReAct (Reasoning + Acting) pattern.

Interleaves reasoning traces with tool actions in a Thought-Observation loop. Tool invocations are handled by the native tool-calling system of MultiTurnConversation, while this handler adds structured reasoning around the process.

public sealed class ReActHandler : PlanningHandlerBase, IPlanningHandler
Inheritance
ReActHandler
Implements
Inherited Members

Examples

Example: Create an agent with ReAct planning

using LMKit.Model;
using LMKit.Agents;
using LMKit.Agents.Tools;
using System;
using System.Threading.Tasks;

// Define tools for the agent
public class SearchTools
{
    [LMFunction("search_web", "Search the web for information")]
    public string SearchWeb(string query)
    {
        return $"Search results for: {query}";
    }

    [LMFunction("calculate", "Perform a calculation")]
    public double Calculate(string expression)
    {
        // Simple calculation logic
        return 42.0;
    }
}

LM model = LM.LoadFromModelID("llama-3.2-3b");

// Register tools
var tools = new ToolRegistry();
tools.Register(new SearchTools());

// Build agent with ReAct planning strategy
Agent agent = new AgentBuilder()
    .WithModel(model)
    .WithPersona("Research Assistant")
    .WithInstruction("Use available tools to answer questions. Think step by step.")
    .WithPlanning(PlanningStrategy.ReAct)
    .WithTools(tools)
    .Build();

// Execute - the agent will use Thought/Observation loop with native tool calls
using var executor = new AgentExecutor();
var result = await executor.ExecuteAsync(agent, "What is the population of France?");

Console.WriteLine($"Final Answer: {result.Content}");
Console.WriteLine($"Reasoning: {result.ReasoningTrace}");

Remarks

ReAct Loop

  1. Thought: The agent reasons about the current state and what to do next.
  2. Tool Call: The agent uses the native JSON tool-calling format to invoke tools.
  3. Observation: The result of the tool call is recorded in the reasoning trace.
  4. Repeat until the agent produces a Final Answer.

Output Format
The handler expects output in the format:

Thought: [reasoning about what to do]
[Native tool call JSON or direct response]
Or for final answers:
Thought: [final reasoning]
Final Answer: [response to user]

Tool Calling
Tool invocations use the native JSON format handled by MultiTurnConversation. This ensures compatibility with all model families and their specific tool-calling implementations. The ReAct handler wraps tool results as "Observation:" entries in the reasoning trace.

Constructors

ReActHandler()

Initializes a new instance of the ReActHandler class.

Fields

FinalAnswerMarker

Marker for the final answer section.

ObservationMarker

Marker for the observation section (tool results).

ThoughtMarker

Marker for the thought/reasoning section.

Properties

Instance

Gets the singleton instance with default settings.

Strategy

Gets the planning strategy.

Methods

Initialize(PlanningContext)

Initializes the context before the first inference call.

PrepareInput(PlanningContext, string)

Prepares the input by adding ReAct reasoning format instructions.

Note: Tool calling format is NOT included here - it's handled by the native tool-calling system in MultiTurnConversation.

PrepareNextInput(PlanningContext)

Prepares the next input after receiving an observation (tool result).

ProcessOutput(PlanningContext, string)

Processes the model output by parsing ReAct components.

RecordObservation(PlanningContext, string, string)

Records a tool result as an observation in the planning context.

Call this method after a tool invocation to include the result in the ReAct reasoning trace. This is typically done by subscribing to the AfterToolInvocation event.

SetObservation(PlanningContext, string)

Sets the observation from a tool result.