Class PlanningContext
Provides context and state for planning strategy execution.
The context is passed to IPlanningHandler methods and contains the agent configuration, execution state, and accumulated results.
public sealed class PlanningContext
- Inheritance
-
PlanningContext
- Inherited Members
Examples
Example: Access planning context during agent execution
using LMKit.Model;
using LMKit.Agents;
using LMKit.Agents.Planning;
using System;
using System.Threading.Tasks;
LM model = LM.LoadFromModelID("llama-3.2-3b");
// Build agent with ReAct planning
Agent agent = new AgentBuilder()
.WithModel(model)
.WithPersona("Research Assistant")
.WithPlanning(PlanningStrategy.ReAct)
.Build();
using var executor = new AgentExecutor();
// Execute and access planning context
var result = await executor.ExecuteAsync(agent, "Analyze this problem step by step.");
// Access planning context after execution
PlanningContext context = executor.PlanningContext;
if (context != null)
{
Console.WriteLine($"Iterations: {context.Iteration}");
Console.WriteLine($"Steps: {context.Steps.Count}");
Console.WriteLine($"Reasoning trace:\n{context.GetReasoningTrace()}");
}
Example: Store custom state in planning context
using LMKit.Agents.Planning;
using System;
// During custom planning handler implementation
void ProcessStep(PlanningContext context)
{
// Store intermediate results
context.SetState("processedItems", 5);
context.SetState("confidence", 0.95);
// Retrieve state later
int items = context.GetState<int>("processedItems");
double confidence = context.GetState<double>("confidence", 0.0);
// Append to reasoning trace
context.AppendReasoningTrace($"Processed {items} items with {confidence:P0} confidence");
// Check iteration limits
if (context.MaxIterationsReached)
{
Console.WriteLine("Max iterations reached, finalizing...");
}
}
Remarks
Lifecycle
A new context is created for each agent execution. It accumulates state
across planning iterations and is disposed when execution completes.
Thread Safety
This class is not thread-safe and should only be accessed from a single execution thread.
Properties
- Agent
Gets the agent being executed.
- CurrentAction
Gets or sets the current action being taken.
- CurrentThought
Gets or sets the current thought or reasoning step.
- FinalAnswer
Gets or sets the final answer when planning is complete.
- Iteration
Gets the current iteration number (1-based).
- LastObservation
Gets or sets the most recent observation (e.g., tool result).
- LastOutput
Gets or sets the most recent model output.
- MaxIterations
Gets the maximum allowed iterations.
- MaxIterationsReached
Gets a value indicating whether the maximum iterations have been reached.
- OriginalInput
Gets the original user input.
- Steps
Gets the list of completed planning steps.
- ToolCalls
Gets the tool calls made during the current planning cycle.
Methods
- AppendReasoningTrace(string)
Appends content to the reasoning trace.
- GetReasoningTrace()
Gets the accumulated reasoning trace.
- GetState<T>(string, T)
Retrieves a value from the context state.
- HasState(string)
Checks if a state key exists.
- SetState(string, object)
Stores a value in the context state.