What is an Agent?
An Agent is the central runtime orchestrator in AgentLIB. It coordinates the entire execution flow: loading memory, invoking the reasoning engine, executing tools, running middleware, and emitting events. Think of an agent as a container that brings together:- Model Provider - The LLM that powers reasoning
- Tools - Functions the agent can call
- Memory - Conversation history persistence
- Reasoning Engine - The strategy for decision-making
- Middleware - Lifecycle hooks for cross-cutting concerns
- Policy - Resource limits and constraints
Creating an Agent
AgentLIB offers two ways to create agents: object-based and class-based.Object-Based Configuration
The simplest approach uses a configuration object:Class-Based Configuration (Decorators)
For complex agents with many tools, use decorators:Decorators require
reflect-metadata and TypeScript’s experimentalDecorators enabled.Agent Lifecycle
When you callagent.run(), the following sequence occurs:
Step-by-Step Breakdown
1. Context Creation
1. Context Creation
A fresh
ExecutionContext is created for each run, containing:- User input
- Custom data state
- Empty message/step arrays
- Session ID for memory scoping
- Event emitter reference
packages/core/src/context/factory.ts:32-542. Event: run:start
2. Event: run:start
Emits the
run:start event with the user input and session ID.3. Middleware: run:before
3. Middleware: run:before
Runs all middleware registered for the
run:before scope.4. Memory Loading
4. Memory Loading
If a memory provider is configured, it reads prior conversation history for the session and injects it into
ctx.state.messages.Source: packages/core/src/agent/agent.ts:123-1275. Reasoning Engine Execution
5. Reasoning Engine Execution
The configured
ReasoningEngine takes over and implements its strategy (ReAct, planning, chain-of-thought, etc.). The engine:- Calls the model
- Executes tools as needed
- Pushes reasoning steps for observability
- Returns the final output string
packages/core/src/agent/agent.ts:1356. Memory Persistence
6. Memory Persistence
If a memory provider is configured, the full conversation (including tool calls and results) is persisted.Source:
packages/core/src/agent/agent.ts:138-1437. Middleware: run:after
7. Middleware: run:after
Runs all middleware registered for the
run:after scope.8. Event: run:end
8. Event: run:end
Emits the
run:end event with the final output and execution state.Agent Configuration
TheAgentConfig interface defines all available options:
Configuration Options
| Option | Type | Description |
|---|---|---|
name | string | Required. Unique identifier for the agent |
description | string | Human-readable description |
model | ModelProvider | LLM provider (e.g., OpenAI, Anthropic) |
tools | ToolDefinition[] | Functions the agent can call |
memory | MemoryProvider | Conversation persistence strategy |
reasoning | ReasoningStrategy | ReasoningEngine | Decision-making strategy (default: 'react') |
middleware | Middleware[] | Lifecycle hooks |
policy | AgentPolicy | Resource limits and constraints |
systemPrompt | string | Injected as the first message in every run |
data | TData | Custom typed state shared across execution |
Fluent API
Agents support a chainable fluent API for configuration:packages/core/src/agent/agent.ts:55-68
Custom Data State
Thedata field allows you to define custom typed state that’s accessible in tools and middleware:
Session Management
Sessions enable multi-turn conversations with memory:If you don’t provide a
sessionId, a random UUID is generated per run, creating isolated single-turn conversations.Cancellation
You can cancel a running agent using anAbortSignal:
RunResult
Everyagent.run() returns a RunResult containing the output and full execution state:
Best Practices
- Use descriptive names - Helps with debugging and observability
- Set reasonable policies - Prevent runaway costs with
maxSteps,timeout,maxCost - Leverage sessions - Use consistent
sessionIdvalues for multi-turn conversations - Type your data - Use TypeScript generics for type-safe custom state
- Handle errors - Always wrap
agent.run()in try/catch
Next Steps
- Tools - Learn how to give agents capabilities
- Memory - Understand conversation persistence
- Reasoning - Explore different reasoning strategies
- Middleware - Add lifecycle hooks
- Events - Observe agent execution