Agent
The Agent pattern provides a robust and flexible way to build autonomous AI systems that can reason, plan, and interact with their environment. It's particularly well-suited for tasks requiring multiple steps, tool use, and dynamic decision-making.

Nodes of an Agent
An agent typically consists of the following components:
Planner Node:
Role: Decides the next action based on the current state and the overall goal. This often involves an LLM call to generate a plan or select a tool.
Output: Triggers an action corresponding to the chosen next step (e.g.,
"use_tool","respond","replan").
Tool Nodes:
Role: Implement specific functionalities (e.g., search, calculator, API calls, code execution).
Execution: Called by the Planner Node.
Output: Return results that the Planner Node can use for subsequent decisions.
Responder Node:
Role: Formulates the final response to the user or takes a concluding action.
Execution: Triggered when the Planner Node determines the task is complete.
Agent Flow
The typical flow of an agent involves a loop:
Observe: The agent receives an input (e.g., user query).
Plan: The Planner Node analyzes the input and current memory to decide the next action.
Act: The agent executes the chosen action (e.g., calls a tool, generates a response).
Reflect/Loop: The result of the action is fed back into the memory, and the Planner Node re-evaluates the situation, potentially leading to further planning and action until the goal is achieved.
The core of building high-performance and reliable agents boils down to:
Context Management: Provide relevant, minimal context. For example, rather than including an entire chat history, retrieve the most relevant via RAG. Even with larger context windows, LLMs might still fall victim to "lost in the middle", overlooking mid-prompt content.
Action Space: Provide a well-structured and unambiguous set of actions—avoiding overlap like separate
read_databasesorread_csvs. Instead, import CSVs into the database.
Best Practices
Incremental: Feed content in manageable chunks (500 lines or 1 page) instead of all at once.
Overview-zoom-in: First provide high-level structure (table of contents, summary), then allow drilling into details (raw texts).
Parameterized/Programmable: Instead of fixed actions, enable parameterized (columns to select) or programmable (SQL queries) actions, for example, to read CSV files.
Backtracking: Let the agent undo the last step instead of restarting entirely, preserving progress when encountering errors or dead ends.
Example: Search Agent (with Tool Use)
Let's create a basic agent that can answer questions by either directly responding or using a "search" tool.
This agent:
Decides whether to search or answer
If searches, loops back to decide if more search needed
Answers when enough context gathered
For simplicity, these will be overly-simplified mock tools/nodes. For a more in-depth implementation, check the implementations in our cookbook for Python or TypeScript.
1. Define Tool Nodes
First, we define (mock) our tool nodes.
2. Define Agent Nodes and Flow
{% endtab %}
{% tab title="TypeScript" %}
Last updated