1. What Is an AI Agent
An AI Agent (artificial intelligence agent) is an intelligent system capable of autonomously executing tasks around a goal.
Traditional AI applications usually follow a fixed workflow: the user inputs a question, the model generates a result, and the system returns a response.
An AI Agent, by contrast, is not only responsible for generating content; it can also decide what to do next based on the goal, call external tools to obtain information or perform actions, and adjust subsequent behavior based on execution results.
For example, a user asks:
Help me analyze the current project's technical debt, identify the three issues that should be prioritized most, and generate an improvement plan.
A regular LLM application may directly generate an analysis report based on the context provided by the user.
An AI Agent, however, may perform the following:
- Understand the user's goals and constraints.
- Inspect the project directory and code structure.
- Analyze dependencies and code quality.
- Call static analysis tools.
- Check test coverage and build results.
- Summarize issues and assess priorities.
- Formulate an improvement plan based on the analysis results.
- Generate the final report.
Throughout the process, the Agent is not merely generating text; it is continuously perceiving the environment, making decisions, and executing actions.
1.1 Core Definition of an Agent
An AI Agent can be defined as:
An intelligent system capable of perceiving its environment, understanding goals, formulating action plans, invoking tools to execute tasks, and continuously adjusting its behavior based on feedback.
It typically has the following five core capabilities:
| Capability | Meaning | Typical manifestation |
|---|---|---|
| Perception | Acquire environment and task information | Read files, receive user input |
| Reasoning | Analyze problems and determine the next action | Identify task dependencies, analyze solutions |
| Planning | Decompose goals into executable steps | Generate task plans |
| Action | Execute operations or invoke tools | Search, run code, access APIs |
| Feedback | Adjust strategy based on execution results | Retry, revise plans, terminate tasks |
Together, these capabilities form the basic operating mechanism of an Agent.
1.2 Differences Between Agents and Traditional Programs
Traditional programs usually execute according to logic predefined by developers.
For example:
def process_order(order):
validate_order(order)
calculate_price(order)
create_payment(order)
send_notification(order)
The program's execution path is determined by code, and it typically does not autonomously change task goals or choose new tools.
An AI Agent, however, can dynamically determine execution steps based on task content.
For example:
def agent(user_goal):
context = perceive(user_goal)
while not context.goal_completed:
plan = reason_and_plan(context)
action = select_action(plan)
result = execute(action)
context = update_context(context, result)
return generate_final_result(context)
This code is only a simplified execution model; a real Agent also needs to handle issues such as tool permissions, execution state, exceptions, context windows, loop termination, and human confirmation.
The main differences between the two are:
| Dimension | Traditional program | AI Agent |
|---|---|---|
| Execution logic | Predefined rules | Model participates in dynamic decision-making |
| Process structure | Relatively fixed | Can be adjusted dynamically |
| Input handling | According to a fixed schema | Can understand natural language and multimodal input |
| Tool selection | Specified by developers | Can be selected based on the task |
| Environmental feedback | Explicitly handled by the program | Can be judged in conjunction with the model |
| Exception handling | Preset branches | Can choose recovery strategies based on feedback |
| Degree of autonomy | Determined by code logic | Constrained by model, tools, and permissions |
Note that an AI Agent is not an autonomous entity completely detached from program control.
Its scope of execution is still limited by system prompts, tool definitions, permission policies, runtime constraints, and business rules.
2. The Relationship Between LLMs, AI Applications, and AI Agents
To understand AI Agents, we first need to distinguish several easily confused concepts.
2.1 LLM: Large Language Model
An LLM (Large Language Model) is one of the core intelligent components of an Agent.
It is responsible for:
- Understanding natural language.
- Analyzing context.
- Generating text.
- Identifying task intent.
- Reasoning and planning.
- Generating structured output.
- Determining whether tools need to be invoked.
However, an LLM itself typically does not have the ability to directly access external systems.
The model can generate a SQL statement, but it is the database connection component that actually executes the SQL; the model can generate HTTP request parameters, but it is the tool execution layer that actually accesses external services.
Therefore, an LLM is more like the decision-making and reasoning engine of an Agent.
2.2 AI Application: AI Applications
An AI Application is a complete application built around a model.
It usually includes:
- A user interaction interface.
- Prompt management.
- Model invocation.
- Context management.
- Data retrieval.
- Business logic.
- Result presentation.
Not all AI applications are Agents.
For example, a simple text summarization service:
User inputs article
↓
Call LLM
↓
Generate summary
↓
Return result
Although it uses a large model, its execution flow is basically fixed and does not require autonomous planning or dynamic action selection.
2.3 AI Agent: An AI System with Action Capabilities
An Agent adds dynamic decision-making and action capabilities on top of an AI application.
flowchart TD
A["User goal"] --> B["AI Application"]
B --> C["Call LLM"]
C --> D{"Is autonomous action required?"}
D -->|"No"| E["Generate and return result"]
D -->|"Yes"| F["Agent Runtime"]
F --> G["Task planning"]
G --> H["Tool selection and execution"]
H --> I["Obtain environment feedback"]
I --> J{"Is the goal complete?"}
J -->|"No"| G
J -->|"Yes"| K["Aggregate results and return"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style E fill:#E5F0E9,stroke:#83A995,color:#315D49
style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style J fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style K fill:#E5F0E9,stroke:#83A995,color:#315D49
The relationship among the three can be summarized as:
- LLM provides language understanding, reasoning, and generation capabilities.
- AI Application encapsulates model capabilities into usable business functions.
- AI Agent introduces dynamic planning, tool execution, and feedback control on top of the application.
III. Core Architecture of AI Agents
A production-grade AI Agent is usually not a single model, but a runtime system composed of multiple collaborating components.
3.1 Overall Architecture
A typical architecture can be divided into seven parts:
| Component | Core Responsibility |
|---|---|
| Agent Interface | Receives user requests and displays execution status and results |
| Agent Runtime | Manages the Agent lifecycle and execution loop |
| LLM Engine | Responsible for reasoning, planning, and decision-making |
| Tool Registry | Manages available tools and their definitions |
| Tool Executor | Executes tool calls selected by the model |
| Memory System | Stores short-term context and long-term memory |
| State Manager | Manages task state, execution records, and checkpoints |
Architecture relationships:
flowchart TB
A["User / External Events"] --> B["Agent Interface"]
B --> C["Agent Runtime"]
C --> D["LLM Engine"]
C --> E["Memory System"]
C --> F["State Manager"]
D --> G["Tool Registry"]
G --> H["Tool Executor"]
H --> I["External Tools and Environment"]
I --> H
H --> C
E --> D
F --> C
C --> J["Result Output"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style D fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#EAF3EC,stroke:#83A995,color:#315D49
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#E5F0E9,stroke:#83A995,color:#315D49
3.2 Agent Runtime
Agent Runtime is the execution control center of the entire Agent system.
It coordinates models, tools, memory, and task state.
Core responsibilities include:
- Receive user tasks.
- Initialize the execution context.
- Invoke the model for decision-making.
- Parse the action instructions returned by the model.
- Schedule tool execution.
- Feed tool results back to the model.
- Determine whether to continue execution.
- Manage timeouts and the maximum number of loops.
- Save execution state.
- Handle cancellation and exceptions.
Runtime should not delegate all execution logic to the LLM.
The model is responsible for proposing action suggestions, while Runtime is responsible for validation and execution.
For example, if the model requests to delete a file, Runtime must check the file path, user permissions, and operation risk, rather than directly executing the instruction generated by the model.
3.3 LLM Engine
LLM Engine is the Agent's reasoning engine.
It typically handles:
- Understanding task intent.
- Context analysis.
- Task decomposition.
- Action selection.
- Parameter generation.
- Tool-call decision-making.
- Result evaluation.
- Final answer generation.
Note that model output is not equivalent to a trusted execution instruction.
Even if the model generates parameters that conform to the JSON format, they must still undergo schema validation, permission validation, and business rule checks.
IV. Core Operating Mechanisms of AI Agents
The most important characteristic of an Agent is not the use of a large model, but the formation of a continuously running decision-and-execution loop.
4.1 Agent Loop
A typical Agent Loop includes the following steps:
- Observe: Observe the current environment and task state.
- Think: Analyze the problem and existing information.
- Plan: Formulate the next-step plan.
- Act: Execute tool or environment operations.
- Evaluate: Assess the execution result.
- Repeat: Continue execution or end the task based on feedback.
flowchart TD
A["Receive Task"] --> B["Observe: Perceive Environment"]
B --> C["Think: Analyze and Reason"]
C --> D["Plan: Formulate a Plan"]
D --> E["Act: Execute Actions"]
E --> F["Evaluate: Assess Results"]
F --> G{"Is the Goal Completed?"}
G -->|"No"| B
G -->|"Yes"| H["Generate Final Result"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style D fill:#EAF3EC,stroke:#83A995,color:#315D49
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style G fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style H fill:#E5F0E9,stroke:#83A995,color:#315D49
This is an abstract execution model.
In actual implementations, Observe, Think, Plan, and Evaluate may not each invoke the model separately; they may also be combined into a single model call.
The key is that the Agent must be able to adjust subsequent actions based on actual execution results, rather than always following a fixed sequence of steps.
4.2 Agent Execution State
An Agent cannot simply keep a conversation history.
Complex tasks require explicit state management.
For example:
{
"run_id": "run_10001",
"task": "Analyze project technical debt",
"status": "RUNNING",
"current_step": "ANALYZE_DEPENDENCIES",
"plan": [
{
"step": 1,
"name": "Read project structure",
"status": "COMPLETED"
},
{
"step": 2,
"name": "Analyze dependencies",
"status": "RUNNING"
},
{
"step": 3,
"name": "Check test coverage",
"status": "PENDING"
}
],
"tool_calls": [],
"observations": [],
"iteration": 2
}
State information typically includes:
- Task objective.
- Task constraints.
- Current plan.
- Completed steps.
- Pending steps.
- Tool call records.
- Tool results.
- Current execution iteration.
- Failure reasons.
- Final completion criteria.
In this way, even if task execution is interrupted, it can resume from a checkpoint instead of starting over from the beginning.
5. Agent Planning Mechanisms
Planning is an important capability that distinguishes AI Agents from simple tool-calling systems.
It is responsible for converting a complex goal into executable task steps.
5.1 Task Decomposition
Suppose the user proposes:
Analyze a technical project, identify performance bottlenecks, and propose an optimization plan.
This goal is too broad to correspond directly to a single tool call.
The Agent needs to break it down into:
flowchart TD
A["Analyze project performance"] --> B["Obtain project code"]
B --> C["Identify system architecture"]
C --> D["Analyze critical call chains"]
D --> E["Check CPU and memory usage"]
E --> F["Analyze database access"]
F --> G["Locate performance bottlenecks"]
G --> H["Evaluate optimization options"]
H --> I["Generate optimization report"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#EAF3EC,stroke:#83A995,color:#315D49
style D fill:#EAF3EC,stroke:#83A995,color:#315D49
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style H fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style I fill:#E5F0E9,stroke:#83A995,color:#315D49
The task steps here are not necessarily completely fixed.
For example, if it finds that the project has no performance monitoring data, the Agent may need to first look for logs or configuration files; if it finds obvious problems with database queries, it can further analyze SQL execution plans.
This is the difference between dynamic planning and fixed workflows.
5.2 Common Planning Strategies
1. Sequential Planning
Tasks are executed in sequence.
This is suitable for tasks with clear dependencies.
For example:
- Read the file.
- Extract the content.
- Analyze the content.
- Generate a report.
2. Parallel Planning
Multiple independent tasks can be executed simultaneously.
For example:
- Analyze code structure.
- Check dependency vulnerabilities.
- Measure test coverage.
These tasks can be processed in parallel by different Workers.
3. Conditional Planning
Subsequent execution paths are determined based on intermediate results.
For example:
- If security vulnerabilities are found, enter the security analysis process.
- If no vulnerabilities are found, continue checking performance issues.
4. Iterative Planning
After executing a step, re-plan based on the results.
Suitable for exploratory tasks, debugging tasks, and open-ended research tasks.
5.3 The Difference Between Planning and Workflows
| Dimension | Fixed workflow | Agent dynamic planning |
|---|---|---|
| Execution path | Predefined | Generated based on task and feedback |
| Conditional branching | Defined by developer | Model participates in decision-making |
| Task decomposition | Usually fixed | Can be dynamically adjusted |
| Tool selection | Usually pre-specified | Can be dynamically selected |
| Predictability | Relatively high | Relatively low |
| Execution cost | Easy to estimate | May incur additional reasoning cost |
| Applicable scenarios | Standardized business processes | Open-ended complex tasks |
Note that Agent does not mean all tasks should use dynamic planning.
For highly constrained businesses such as payments, orders, approvals, and fund operations, fixed workflows are usually more reliable.
Real-world systems often adopt a hybrid architecture: fixed workflows handle critical control, while the Agent handles complex judgment and non-deterministic tasks.
6. Tool Invocation Mechanism (Tool Calling)
Tool calling is a key capability that enables AI Agents to move from language generation to real-world action.
LLMs themselves usually cannot directly access file systems, databases, browsers, or business systems.
Tool Calling is needed to convert model decisions into external operations.
6.1 What Is a Tool
A tool is an external capability that an Agent can invoke.
Common tools include:
| Tool category | Typical capability |
|---|---|
| Search tools | Web Search, knowledge base retrieval |
| File tools | File reading, file writing |
| Data tools | SQL querying, data analysis |
| Code tools | Python execution, Shell execution |
| Browser tools | Page access, clicking, and form operations |
| Communication tools | Email sending, message notifications |
| Business tools | Create orders, query accounts, approvals |
| System tools | Get time, read system status |
A tool needs to have a clear name, description, parameter structure, and execution logic.
6.2 Tool Definition
A tool usually consists of the following parts:
- Tool Name: The name of the tool.
- Description: A description of the tool's purpose.
- Parameters: Input parameters.
- Schema: Parameter structure and type constraints.
- Handler: The actual execution function.
- Permission: Invocation permission.
- Timeout: Execution timeout.
For example, define a weather query tool:
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
}
After the model sees the tool definition, it can determine whether to invoke it based on the user's request.
6.3 The Complete Process of Tool Calling
flowchart TD
A["User submits a request"] --> B["Agent calls LLM"]
B --> C["Model analyzes available tools"]
C --> D{"Is a tool needed?"}
D -->|"No"| E["Generate plain text"]
D -->|"Yes"| F["Return tool call request"]
F --> G["Runtime parses parameters"]
G --> H["Schema validation"]
H --> I["Permission and risk check"]
I --> J{"Is execution allowed?"}
J -->|"No"| K["Reject or request human confirmation"]
J -->|"Yes"| L["Tool Executor executes"]
L --> M["Obtain tool result"]
M --> N["Return result to LLM"]
N --> O{"Is another call needed?"}
O -->|"Yes"| C
O -->|"No"| P["Generate final answer"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style C fill:#EAF3EC,stroke:#83A995,color:#315D49
style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style E fill:#E5F0E9,stroke:#83A995,color:#315D49
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style K fill:#F7EAE7,stroke:#C98D7C,color:#754438
style L fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style M fill:#EAF3EC,stroke:#83A995,color:#315D49
style N fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style O fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style P fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Note that the model returning a tool call request does not mean the tool has already been executed.
The complete process includes at least:
- The model generates a tool call intent.
- Runtime identifies the tool name.
- Parameter parsing and Schema validation.
- Permission and risk checks.
- Execute the actual tool function.
- Obtain the execution result.
- Put the result back into the model context.
- The model continues reasoning or generates the final answer.
6.4 The Difference Between Tool Calling and Plain Text Generation
Plain text generation:
Input Prompt
↓
LLM generates text
↓
Return text
Tool calling:
Input Prompt
↓
LLM generates tool call request
↓
Runtime executes tool
↓
Obtain actual execution result
↓
LLM continues processing based on the result
The biggest difference between the two is: tool calling involves external side effects or interaction with the external environment.
Therefore, tool execution must have an independent security control layer.
6.5 Key Engineering Issues in Tool Calling
Parameter trustworthiness
The model may generate incorrect parameters, so Schema validation must be performed.
Permission control
The fact that the model has selected a tool does not mean the user has permission to execute it.
Timeout control
Tool calls need a timeout to prevent a single operation from occupying the Agent for a long time.
Idempotency
For operations such as creating orders and sending emails, repeated execution should be prevented.
Side-effect management
Operations such as writes, deletions, payments, and publishing require stricter risk control.
Result verification
A tool returning a success status does not necessarily mean the business goal has actually been achieved. The actual result needs to be checked.
7. Agent's Memory System (Memory)
An Agent needs a memory system to store task context, historical information, and long-term knowledge.
If only the current user input is passed each time the model is called, the Agent cannot effectively handle cross-step and cross-session tasks.
7.1 Why Agents Need Memory
For example, the user requests:
Help me keep following up on this project, record every new issue as it is found, and eventually produce a complete technical debt report.
The Agent needs to remember:
- Basic project information.
- Modules that have been analyzed.
- Issues that have been discovered.
- Tool operations that have been executed.
- Constraints specified by the user.
- Tasks that have not yet been completed.
- Historical analysis results.
This information cannot all rely on a single Prompt.
7.2 Short-Term Memory and Long-Term Memory
Short-term Memory
It primarily stores information generated during the execution of the current task.
For example:
- Current conversation context.
- Current task goal.
- Recent rounds of tool calls.
- Current plan.
- Current execution status.
- Intermediate results.
Short-term memory is usually bound to the current Agent Run or Session.
Long-term Memory
It is used to store information that remains valuable across tasks and sessions.
For example:
- User preferences.
- Project background.
- Long-term task constraints.
- Confirmed business rules.
- Historical task summaries.
- Reusable experiential knowledge.
Long-term memory usually requires independent storage and retrieval.
7.3 Composition of the Memory System
| Memory Type | Main Purpose | Storage Method |
|---|---|---|
| Conversation memory | Store conversation context | Message storage |
| Task memory | Store execution progress | State database |
| Semantic memory | Store facts and knowledge | Database, vector store |
| Episodic memory | Store historical task experiences | Event records, summaries |
| Procedural memory | Store reusable execution methods | Workflows, skill definitions |
7.4 Memory Retrieval and Update
Long-term memory should not be unconditionally stuffed entirely into the Prompt.
It usually needs to be retrieved, filtered, and compressed based on the current task.
flowchart TD
A["Receive new task"] --> B["Extract task context"]
B --> C["Generate memory retrieval conditions"]
C --> D["Query long-term memory"]
D --> E["Filter by relevance and permissions"]
E --> F["Rank and compress memories"]
F --> G["Build Agent Context"]
G --> H["Execute task"]
H --> I["Extract new useful information"]
I --> J{"Is it worth long-term storage?"}
J -->|"Yes"| K["Update long-term memory"]
J -->|"No"| L["Keep only task records"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style D fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style E fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#EAF3EC,stroke:#83A995,color:#315D49
style J fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style K fill:#E5F0E9,stroke:#83A995,color:#315D49
style L fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
The memory system requires special attention to:
- Memory sources must be traceable.
- Untrusted input must not be directly promoted to long-term facts.
- Expired information needs to be updated or invalidated.
- Different users and tenants must be isolated from each other.
- Sensitive information must comply with access and retention policies.
- Memory writes should have a quality assessment mechanism.
7.5 Differences Between Memory and RAG
Memory and RAG often appear together in Agent architectures, but they solve different problems.
| Dimension | Memory | RAG |
|---|---|---|
| Core purpose | Preserve the Agent's historical state and experience | Retrieve external knowledge |
| Main content | User preferences, task status, historical experiences | Documents, databases, business knowledge |
| Data source | Agent runtime process and explicitly written information | External knowledge base |
| Update method | Memory extraction, merging, and eviction | Data synchronization, parsing, and indexing |
| When used | Restore context, continue tasks | Answer questions, obtain external evidence |
Memory can use vector retrieval, and RAG can also use databases, but the two should not be regarded as the same concept just because they use the same storage technology.
VIII. Context Management for AI Agents
The execution of an Agent usually generates a large amount of information.
If all content is continuously kept in the model context, it will lead to increased Token costs, attention dilution, and context window overflow.
Therefore, dedicated context management mechanisms are needed.
8.1 Context Composition
The context of a model call usually includes:
System Instructions
+
User Request
+
Task State
+
Conversation History
+
Retrieved Memory
+
Available Tools
+
Recent Tool Results
Among these, tool definitions and tool return results also consume the context window.
8.2 Context Compression
Common context management approaches:
- Retain the most recent conversation.
- Summarize historical conversation.
- Retain only memories relevant to the current goal.
- Remove irrelevant tool return content.
- Save large results to external storage and retain only references.
- Compress completed tasks into phase summaries.
For example, when an Agent performs a large code analysis task, it does not need to keep the full contents of all source files in the Prompt at all times.
The code analysis results can be stored in external task storage, and only key conclusions and necessary file references need to be passed in during subsequent reasoning.
8.3 Context and Task State Must Not Be Confused
This is a very important design issue in Agent engineering.
Context primarily serves model reasoning, while State is responsible for recording the system's actual execution progress.
The two differ as follows:
| Dimension | Context | State |
|---|---|---|
| Primary purpose | Help the model understand the current task | Control task execution |
| Content | Conversation, summaries, memory, tool results | State, steps, execution records |
| Can it be compressed? | Yes | Necessary facts must be retained |
| Does it require persistence? | Depends on the task | Usually required for long-running tasks |
| Does it serve as an execution basis? | Cannot be used alone as a trustworthy basis | Yes, it is an important basis at runtime |
Task completion cannot be determined solely based on model context.
For example, the model may see “the file has been saved successfully” in the history messages, but the actual file write operation may have failed. The final state should be based on the tool execution records and the actual resource state.
IX. Agent Action and Feedback Mechanisms
After an Agent interacts with the external environment, it must handle execution feedback.
If a tool returns an error, data is incomplete, or the result does not meet expectations, the Agent should be able to adjust its actions.
9.1 Classification of Action Results
| Result type | Example | Follow-up handling |
|---|---|---|
| Success | Query returns normal data | Continue the task |
| No data | Search has no results | Change query conditions |
| Parameter error | API parameters do not meet requirements | Correct parameters |
| Permission error | User has no permission to access the resource | Deny or request authorization |
| Temporary error | Network timeout | Retry |
| Business failure | Order creation fails | Analyze the business cause |
| Partial success | Batch operation partially completed | Identify successful and failed items |
9.2 Feedback-Driven Adjustment
flowchart TD
A["Execute tool operation"] --> B["Obtain execution result"]
B --> C{"Result type"}
C -->|"Success"| D["Verify whether the goal is achieved"]
C -->|"Parameter error"| E["Fix parameters"]
C -->|"Temporary error"| F["Retry a limited number of times"]
C -->|"Permission error"| G["Stop the operation or request authorization"]
C -->|"Business failure"| H["Analyze the cause of failure"]
D --> I{"Is the task complete?"}
I -->|"No"| J["Re-plan"]
I -->|"Yes"| K["End the task"]
E --> J
F --> J
H --> J
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#F7EAE7,stroke:#C98D7C,color:#754438
style H fill:#F7EAE7,stroke:#C98D7C,color:#754438
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style K fill:#E5F0E9,stroke:#83A995,color:#315D49
The feedback mechanism needs to avoid two extremes:
- Blind retrying: repeatedly executing an error that cannot be recovered from.
- Endless adjustment: the model keeps trying different approaches but still never completes the goal.
Therefore, it is necessary to limit the maximum number of iterations, the number of tool calls, the execution time, and the total cost.
X. Agent Execution Modes and Architectural Forms
There is more than one way for an Agent to run.
Depending on task complexity, the number of tools, and the mode of collaboration, different architectures can be adopted.
10.1 Single-Agent Architecture
The single Agent is the most basic form.
A single Agent is responsible for:
- Understanding the task.
- Planning the steps.
- Selecting tools.
- Executing operations.
- Handling feedback.
- Outputting results.
Suitable for:
- Personal assistants.
- Simple code analysis.
- Document processing.
- Lightweight automation.
- Single-domain tasks.
The advantage is a simple structure, with relatively easy state management and debugging.
The disadvantage is that as task complexity increases, a single Agent's prompt, tool set, and context may become unwieldy.
10.2 Multi-Agent Architecture
A multi-agent system consists of multiple Agents with different responsibilities collaborating to complete a task.
For example, a technical research task can be split into:
- Research Agent: responsible for searching for materials.
- Analysis Agent: responsible for technical analysis.
- Coding Agent: responsible for code verification.
- Review Agent: responsible for reviewing results.
- Coordinator Agent: responsible for coordination and aggregation.