Skip to content

The agent engineering platform.

feat(agents): allow trimming of intermediate steps to last N (#6476)

Nir Gazita8bbfb2da3f8
diff --git a/langchain/agents/agent.py b/langchain/agents/agent.py
index 807101e074..7c3a354b5c 100644
--- a/langchain/agents/agent.py
+++ b/langchain/agents/agent.py
@@ -659,6 +659,9 @@ s
as an argument, and the result of that function will be passed to the agent
as an observation.
"""
+ trim_intermediate_steps: Union[
+ int, Callable[[List[Tuple[AgentAction, str]]], List[Tuple[AgentAction, str]]]
+ ] = -1
@classmethod
def from_agent_and_tools(
@@ -788,6 +791,8 @@ s
Override this to take control of how the agent makes and acts on choices.
"""
try:
+ intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
+
# Call the LLM to see what to do.
output = self.agent.plan(
intermediate_steps,
@@ -879,6 +884,8 @@ s
Override this to take control of how the agent makes and acts on choices.
"""
try:
+ intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
+
# Call the LLM to see what to do.
output = await self.agent.aplan(
intermediate_steps,
@@ -1088,3 +1095,16 @@ s
"",
)
return None
+
+ def _prepare_intermediate_steps(
+ self, intermediate_steps: List[Tuple[AgentAction, str]]
+ ) -> List[Tuple[AgentAction, str]]:
+ if (
+ isinstance(self.trim_intermediate_steps, int)
+ and self.trim_intermediate_steps > 0
+ ):
+ return intermediate_steps[-self.trim_intermediate_steps :]
+ elif callable(self.trim_intermediate_steps):
+ return self.trim_intermediate_steps(intermediate_steps)
+ else:
+ return intermediate_steps