pub trait BaseAgent: Send + Sync {
// Required method
fn plan<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
config: Option<&'life3 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait;
// Provided methods
fn plan_stream<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
on_token: &'life3 mut (dyn FnMut(String) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send),
config: Option<&'life4 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait { ... }
fn input_keys(&self) -> Vec<&str> { ... }
fn get_allowed_tools(&self) -> Option<Vec<&str>> { ... }
fn return_stopped_response(
&self,
_intermediate_steps: &[AgentStep],
) -> AgentFinish { ... }
fn last_token_usage(&self) -> Option<TokenUsage> { ... }
}Expand description
Base Agent trait.
Defines the core interface for agents. Agent is responsible for planning, not execution. Execution is handled by AgentExecutor.
Required Methods§
Sourcefn plan<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
config: Option<&'life3 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn plan<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
config: Option<&'life3 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Plans the next action.
§Arguments
intermediate_steps- History of executed steps.inputs- User input.config- Per-run config for this planning round. The executor supplies a config carrying its callback manager plus trace-linkage metadata, so the LLM provider fireson_llm_*for every planning round and the LLM run joins the agent chain’s trace tree;Nonemeans “no observers configured” and providers dispatch nothing (the pre-0.22.4 behavior). Implementations forward it verbatim tochat/stream_chatand never inspect or strip it.
§Returns
AgentOutput::Action- Action to execute.AgentOutput::Finish- Final answer.
Provided Methods§
Sourcefn plan_stream<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
on_token: &'life3 mut (dyn FnMut(String) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send),
config: Option<&'life4 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
fn plan_stream<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
on_token: &'life3 mut (dyn FnMut(String) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send),
config: Option<&'life4 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Plans the next action, streaming any model text through on_token as it
is produced.
§Arguments
intermediate_steps- History of executed steps.inputs- User input.on_token- Called with each chunk of model text as it becomes available, taking ownership of the chunk (so the returned future never borrows the token and stays'static). Streaming-capable agents (e.g. ReAct, function-calling) emit free text per token; steps that produce no free text (e.g. a function-calling step invoking a tool) emit nothing. Non-streaming agents deliver the whole answer in one call.
§Returns
AgentOutput::Action- Action to execute.AgentOutput::Finish- Final answer.
The default implementation delegates to BaseAgent::plan and forwards
the whole final-answer text as a single chunk — behaviorally identical
to calling plan() directly. Streaming-capable agents (e.g. ReAct)
override this to emit per-token chunks from the model’s streaming chat
API; callers must still call BaseAgent::plan for the non-streaming
path (e.g. invoke) so that path is unaffected.
Sourcefn input_keys(&self) -> Vec<&str>
fn input_keys(&self) -> Vec<&str>
Returns input keys.
Sourcefn get_allowed_tools(&self) -> Option<Vec<&str>>
fn get_allowed_tools(&self) -> Option<Vec<&str>>
Returns allowed tools list.
Sourcefn return_stopped_response(
&self,
_intermediate_steps: &[AgentStep],
) -> AgentFinish
fn return_stopped_response( &self, _intermediate_steps: &[AgentStep], ) -> AgentFinish
Returns stopped response when max iterations reached.
Sourcefn last_token_usage(&self) -> Option<TokenUsage>
fn last_token_usage(&self) -> Option<TokenUsage>
Returns the token usage from the most recent plan() call, if available.
Agents that make LLM calls inside plan() may override this to report
cost metrics to AgentExecutor (P1-5). Defaults to None.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".