⚡ Understanding AI Agent Execution in LM-Kit.NET
📄 TL;DR
AI Agent Execution is the runtime process where an agent interprets user requests, reasons about actions, invokes tools, processes results, and generates responses. It encompasses the agentic loop, the iterative cycle of observation, thought, action, and reflection that enables agents to accomplish complex tasks autonomously. In LM-Kit.NET, agent execution is managed through the Agent class, which orchestrates tool calls, handles conversation context, and streams responses in real-time.
📚 What is Agent Execution?
Definition: Agent Execution is the operational phase where an AI agent actively processes requests and takes actions. Unlike simple question-answering, agent execution involves:
- Reasoning: Determining what needs to be done
- Tool Selection: Choosing appropriate capabilities
- Action Invocation: Executing tools with parameters
- Result Processing: Interpreting tool outputs
- Response Generation: Forming coherent answers
- Iteration: Looping until the task is complete
The Agentic Loop
+---------------------------------------------------------------------------+
| The Agentic Execution Loop |
+---------------------------------------------------------------------------+
| |
| +--------------+ |
| | Observe |◄---------------------+ |
| | (Get Input) | | |
| +------+-------+ | |
| | | |
| v | |
| +--------------+ | |
| | Think | | |
| | (Reason) | | |
| +------+-------+ | |
| | | |
| +---------+---------+ | |
| v v | |
| +------------+ +------------+ | |
| | Answer | | Act | | |
| | (Done) | | (Use Tool) | | |
| +------------+ +------+-----+ | |
| | | |
| v | |
| +------------+ | |
| | Result |--------------+ |
| | (Feedback) | |
| +------------+ |
| |
+---------------------------------------------------------------------------+
🔄 Execution Flow
Step-by-Step Execution
- Input Reception: Agent receives user message
- Context Assembly: Gather conversation history, system prompt, available tools
- Model Inference: LLM generates response (text or tool call)
- Decision Point:
- If text response: Return to user (loop ends)
- If tool call: Execute tool and loop back
- Tool Execution: Run selected tool with parsed arguments
- Result Integration: Add tool output to context
- Continuation: Return to step 3 until done
Basic Execution Example
using LMKit.Model;
using LMKit.Agents;
using LMKit.Agents.Tools.BuiltIn;
var model = LM.LoadFromModelID("gemma3:12b");
// Create agent with tools
var agent = Agent.CreateBuilder(model)
.WithSystemPrompt("You are a helpful assistant with access to web search and calculations.")
.WithTools(tools =>
{
tools.Register(BuiltInTools.WebSearch);
tools.Register(BuiltInTools.Calculator);
})
.Build();
// Execute agent - handles the full agentic loop
var response = await agent.ExecuteAsync(
"What is the population of France and what is that divided by 67 million?",
CancellationToken.None
);
Console.WriteLine(response.Text);
📊 Execution States
Agent Lifecycle
+----------+ +-----------+ +-----------+ +-----------+ +----------+
| Idle |----->| Thinking |----->| Executing |----->|Processing |----->| Complete |
| | | | | Tool | | Result | | |
+----------+ +-----------+ +-----------+ +-----------+ +----------+
▲ | |
| | |
+--------------------------------+----------------+
(Loop continues)
State Descriptions
| State | Description | Agent Activity |
|---|---|---|
| Idle | Waiting for input | No activity |
| Thinking | LLM inference | Generating next action |
| Executing Tool | Running a tool | Tool logic executing |
| Processing Result | Integrating output | Adding result to context |
| Complete | Task finished | Response ready |
⚙️ Execution Configuration
Controlling Execution Behavior
using LMKit.Agents;
var agent = Agent.CreateBuilder(model)
.WithMaxIterations(15) // Maximum tool call rounds
.WithMaxTokens(8192) // Maximum response tokens
.WithTemperature(0.7f) // Creativity level
.WithToolChoice(ToolChoice.Auto) // Let agent decide when to use tools
.Build();
Streaming Execution
using LMKit.Agents;
var agent = Agent.CreateBuilder(model)
.WithTools(tools => tools.Register(BuiltInTools.WebSearch))
.Build();
// Stream responses in real-time
await foreach (var chunk in agent.ExecuteStreamingAsync(
"Research the latest developments in quantum computing",
CancellationToken.None))
{
if (chunk.IsToolCall)
{
Console.WriteLine($"🔧 Calling: {chunk.ToolName}");
}
else
{
Console.Write(chunk.Text);
}
}
Execution with Callbacks
using LMKit.Agents;
var agent = Agent.CreateBuilder(model)
.WithTools(tools => tools.Register(BuiltInTools.Calculator))
.OnToolCallStarted((toolName, args) =>
{
Console.WriteLine($"⚡ Starting: {toolName}");
Console.WriteLine($" Args: {args}");
})
.OnToolCallCompleted((toolName, result) =>
{
Console.WriteLine($"✅ Completed: {toolName}");
Console.WriteLine($" Result: {result}");
})
.OnIterationCompleted((iteration, state) =>
{
Console.WriteLine($"📍 Iteration {iteration} complete");
})
.Build();
var response = await agent.ExecuteAsync(
"Calculate 15% of 847 plus 23% of 1250",
CancellationToken.None
);
🔀 Execution Patterns
1. Single-Shot Execution
One request, one response (may involve multiple tool calls internally):
var response = await agent.ExecuteAsync("What's the weather in Paris?", token);
2. Conversational Execution
Multi-turn with maintained context:
var chat = agent.CreateChat();
var response1 = await chat.SendAsync("Find me restaurants in Tokyo", token);
var response2 = await chat.SendAsync("Which one has the best reviews?", token);
var response3 = await chat.SendAsync("Make a reservation for 2 at 7pm", token);
3. Parallel Tool Execution
Execute independent tools simultaneously:
// Agent can call multiple tools in parallel when they're independent
var response = await agent.ExecuteAsync(
"Get the weather in Paris, London, and Tokyo",
CancellationToken.None
);
// Agent may execute 3 weather API calls in parallel
4. Iterative Refinement
Agent refines results through multiple iterations:
var agent = Agent.CreateBuilder(model)
.WithSystemPrompt("""
You are a research assistant. For complex queries:
1. Search for initial information
2. Identify gaps in the data
3. Search for additional details
4. Synthesize a comprehensive answer
""")
.WithTools(tools => tools.Register(BuiltInTools.WebSearch))
.WithMaxIterations(10)
.Build();
🎯 Execution Strategies
ReAct (Reasoning + Acting)
The most common execution strategy where the agent alternates between reasoning and taking actions:
var agent = Agent.CreateBuilder(model)
.WithPlanningStrategy(PlanningStrategy.ReAct)
.WithTools(tools =>
{
tools.Register(BuiltInTools.WebSearch);
tools.Register(BuiltInTools.Calculator);
})
.Build();
// Agent will:
// Thought: I need to find the current price...
// Action: WebSearch("current gold price")
// Observation: Gold is $2,050 per ounce
// Thought: Now I need to calculate...
// Action: Calculator("2050 * 10")
// Answer: 10 ounces of gold would cost $20,500
Plan-Then-Execute
Create a complete plan before executing any actions:
var agent = Agent.CreateBuilder(model)
.WithPlanningStrategy(PlanningStrategy.PlanAndExecute)
.Build();
// Agent will:
// Plan:
// 1. Search for population data
// 2. Search for GDP data
// 3. Calculate per-capita GDP
// 4. Format and present results
// Execute: [Runs each step]
📖 Key Terms
- Agentic Loop: The iterative observe-think-act-reflect cycle
- Tool Call: A structured request for the agent to invoke a tool
- Iteration: One complete cycle through the agentic loop
- Streaming: Real-time delivery of partial responses
- Context Window: The accumulated history feeding each inference
- Tool Choice: Policy for when/whether to use tools (Auto, Required, None)
📚 Related API Documentation
Agent: Main agent class managing executionAgentBuilder: Fluent builder for agent configurationAgentResponse: Execution result containerToolRegistry: Tool management during execution
🔗 Related Glossary Topics
- AI Agents: The autonomous systems being executed
- AI Agent Planning: How agents plan before/during execution
- AI Agent Tools: Capabilities invoked during execution
- Function Calling: The mechanism for tool invocation
- AI Agent Guardrails: Safety constraints during execution
🌐 External Resources
- ReAct (Yao et al., 2022): Reasoning and Acting in Language Models
- Toolformer (Schick et al., 2023): Teaching LLMs to use tools
- LM-Kit Tool Calling Demo: Agent execution example
📝 Summary
AI Agent Execution is the runtime process that brings agents to life. Through the agentic loop, agents observe inputs, reason about required actions, invoke tools, process results, and iterate until tasks are complete. In LM-Kit.NET, execution is managed through the Agent class, which supports single-shot and conversational modes, streaming responses, parallel tool execution, and configurable iteration limits. Understanding execution patterns like ReAct and Plan-Then-Execute enables developers to build agents that efficiently handle complex, multi-step tasks while maintaining responsiveness and control.