0
# Agents
1
2
Agent framework for autonomous task execution and tool usage. Agents can plan, execute actions, and make decisions based on observations from their environment.
3
4
## Capabilities
5
6
### Agent Actions and Steps
7
8
Core types for representing agent decision-making and execution.
9
10
```typescript { .api }
11
/**
12
* Action taken by an agent to use a tool
13
*/
14
interface AgentAction {
15
/** Name of the tool to execute */
16
tool: string;
17
/** Input to provide to the tool */
18
toolInput: string | Record<string, any>;
19
/** Agent's reasoning log for this action */
20
log: string;
21
}
22
23
/**
24
* Final output when agent completes its task
25
*/
26
interface AgentFinish {
27
/** Final return values from the agent */
28
returnValues: Record<string, any>;
29
/** Agent's final reasoning log */
30
log: string;
31
}
32
33
/**
34
* Single step in agent execution cycle
35
*/
36
interface AgentStep {
37
/** Action that was taken */
38
action: AgentAction;
39
/** Observation result from the action */
40
observation: string;
41
}
42
```
43
44
**Usage Examples:**
45
46
```typescript
47
import { AgentAction, AgentFinish, AgentStep } from "@langchain/core/agents";
48
49
// Example agent action
50
const action: AgentAction = {
51
tool: "calculator",
52
toolInput: { operation: "multiply", a: 23, b: 7 },
53
log: "I need to calculate 23 × 7 to solve this problem"
54
};
55
56
// Example agent step
57
const step: AgentStep = {
58
action: action,
59
observation: "The result of 23 × 7 is 161"
60
};
61
62
// Example agent finish
63
const finish: AgentFinish = {
64
returnValues: { answer: "161", explanation: "The calculation gives us 161" },
65
log: "I have successfully calculated the result and can provide the final answer"
66
};
67
```
68
69
### Agent Planning
70
71
Types for agent planning and intermediate steps.
72
73
```typescript { .api }
74
/**
75
* Intermediate steps in agent execution
76
*/
77
type AgentSteps = AgentStep[];
78
79
/**
80
* Plan generated by an agent
81
*/
82
interface AgentPlan {
83
/** Planned actions */
84
steps: AgentAction[];
85
/** Reasoning for the plan */
86
reasoning: string;
87
}
88
89
/**
90
* Agent execution context
91
*/
92
interface AgentExecutorInput {
93
/** Available tools */
94
tools: StructuredToolInterface[];
95
/** Agent instance */
96
agent: BaseAgent;
97
/** Maximum iterations allowed */
98
maxIterations?: number;
99
/** Early stopping method */
100
earlyStoppingMethod?: "force" | "generate";
101
/** Return intermediate steps */
102
returnIntermediateSteps?: boolean;
103
}
104
```
105
106
## Agent Types
107
108
### Base Agent
109
110
```typescript { .api }
111
/**
112
* Abstract base class for agents
113
*/
114
abstract class BaseAgent {
115
/** Available tools */
116
tools: StructuredToolInterface[];
117
118
constructor(input: { tools: StructuredToolInterface[] });
119
120
/** Plan next action given input and previous steps */
121
abstract plan(
122
steps: AgentStep[],
123
inputs: Record<string, any>,
124
callbacks?: Callbacks
125
): Promise<AgentAction | AgentFinish>;
126
127
/** Get allowed tools for this agent */
128
abstract getAllowedTools(): string[] | undefined;
129
}
130
```
131
132
### Single Action Agent
133
134
```typescript { .api }
135
/**
136
* Agent that takes one action at a time
137
*/
138
abstract class BaseSingleActionAgent extends BaseAgent {
139
/** Plan single next action */
140
abstract plan(
141
steps: AgentStep[],
142
inputs: Record<string, any>,
143
callbacks?: Callbacks
144
): Promise<AgentAction | AgentFinish>;
145
}
146
```
147
148
### Multi Action Agent
149
150
```typescript { .api }
151
/**
152
* Agent that can plan multiple actions
153
*/
154
abstract class BaseMultiActionAgent extends BaseAgent {
155
/** Plan multiple next actions */
156
abstract plan(
157
steps: AgentStep[],
158
inputs: Record<string, any>,
159
callbacks?: Callbacks
160
): Promise<AgentAction[] | AgentFinish>;
161
}
162
```
163
164
## Types
165
166
```typescript { .api }
167
type AgentActionType = AgentAction | AgentAction[];
168
169
interface OutputParserArgs {
170
/** Available tools */
171
tools: StructuredToolInterface[];
172
/** Instructions for tool usage */
173
toolsInstructions?: string;
174
}
175
176
interface AgentArgs {
177
outputParser: AgentActionOutputParser;
178
allowedTools?: string[];
179
}
180
181
abstract class AgentActionOutputParser extends BaseOutputParser<AgentAction | AgentFinish> {
182
abstract parse(text: string): Promise<AgentAction | AgentFinish>;
183
getFormatInstructions(): string;
184
}
185
```