I. What Is a Production-Grade AI Agent?
An Agent that can call search tools, execute code, or query databases does not necessarily mean it has the capabilities required for a production environment.
In a lab environment, an Agent only needs to complete a task once, and occasional failures can simply be re-run. But in production, it has to deal with real users, complex business logic, concurrent requests, external dependency failures, data permissions, and long-running operation.
For example, an enterprise-grade data analysis Agent might need to:
- Understand the user's natural language request.
- Determine whether the user has data access permission.
- Query multiple business databases.
- Call data analysis tools.
- Check whether the computed results are correct.
- Generate an analysis report.
- Save the report to the specified location.
- Record the operation log and return the result.
If any step in this process goes wrong, the task may fail, or worse, data may leak or business losses may occur.
1. Core Characteristics of a Production-Grade Agent
| Characteristic | Specific Requirements |
|---|---|
| Reliability | Tasks can be completed consistently, and exceptions can be recovered from |
| Scalability | Supports multiple users, multiple tasks, and concurrent execution |
| Security | Includes identity authentication, permission control, and risk review |
| Observability | Can trace the execution process of models, tools, and tasks |
| Recoverability | Supports state persistence, resuming from checkpoints, and task replay |
| Controllability | Includes execution budgets, timeouts, cancellation, and human approval |
| Maintainability | Supports version management, automated testing, and canary releases |
| Cost Control | Can manage Token, tool call, and infrastructure costs |
2. Differences Between a Prototype Agent and a Production-Grade Agent
| Dimension | Prototype Agent | Production-Grade Agent |
|---|---|---|
| Task Execution | Single execution | Unified management of long and short tasks |
| State Management | In-memory variables | Persistent state and Checkpoint |
| Tool Invocation | Direct invocation | Unified tool gateway |
| Permission Management | Fixed permissions | User-, task-, and resource-level authorization |
| Exception Handling | Re-run after failure | Classified handling, retry, and recovery |
| Concurrency Control | Simple parallelism | Queues, locks, and leases |
| Quality Assurance | Manual testing | Automated evaluation and regression testing |
| Monitoring | Output logs | Metrics, Logs, Traces |
| Deployment | Local execution | Containerized, elastic deployment |
| Security Governance | Basic input checks | Multi-layered security protection and auditing |
The essence of a production-grade Agent is packaging nondeterministic model capabilities into an engineering system that is manageable, verifiable, and recoverable.
II. Overall Architecture of a Production-Grade AI Agent
In a production environment, large models should not be allowed to connect directly to all databases, business systems, and infrastructure.
A more reasonable approach is to build a layered architecture, where the model is responsible for understanding, planning, and decision-making, while deterministic services handle permissions, execution, state, and verification.
1. Core Architecture Layers
Layer 1: Access Layer (API Gateway)
Responsible for user request access and unified management.
Main functions:
- User identity authentication.
- Request parameter validation.
- Tenant identification.
- Request rate limiting.
- Session management.
- Request routing.
- Streaming responses.
Layer 2: Agent Orchestration Layer (Orchestration)
Responsible for the Agent's task execution and process control.
Main functions:
- Intent recognition.
- Task planning.
- Step scheduling.
- Tool selection.
- Multi-Agent collaboration.
- State transitions.
- Interruption and recovery.
Layer 3: Model Service Layer (Model Gateway)
Unified management of different models.
Main functions:
- Model routing.
- Model fallback.
- Request retries.
- Token budget.
- Model version management.
- Prompt management.
- Model invocation monitoring.
Layer 4: Tool Execution Layer (Tool Gateway)
Responsible for interaction between the Agent and external systems.
Main functions:
- Tool registration.
- Parameter validation.
- Permission checks.
- Tool invocation.
- Timeout control.
- Idempotent handling.
- Result normalization.
Layer 5: Data and State Layer (Data and State)
Stores the data required for Agent execution.
Mainly includes:
- Session data.
- Task state.
- Checkpoint.
- Short-term memory.
- Long-term memory.
- Vector database.
- Business data.
- Audit records.
Layer 6: Governance and Observability Layer (Governance and Observability)
Responsible for the security and operational quality of the entire Agent system.
Mainly includes:
- Permission policies.
- Content safety.
- Human approval.
- Execution auditing.
- Distributed tracing.
- Runtime metrics.
- Quality evaluation.
- Alerting system.
2. Production-Grade Agent Architecture Diagram
flowchart TB
A["User / Business Systems"] --> B["API Gateway"]
B --> C["Identity Authentication & Request Validation"]
C --> D["Agent Orchestrator"]
D --> E["Task Planning & Scheduling"]
D --> F["State Manager"]
D --> G["Model Gateway"]
D --> H["Tool Gateway"]
G --> I["Model Service"]
H --> J["Tool Executor"]
J --> K["Enterprise Business Systems"]
J --> L["External API"]
F --> M["State Store"]
F --> N["Checkpoint Store"]
F --> O["Memory Store"]
D --> P["Policy Engine"]
P --> Q["Permission & Risk Checks"]
Q --> R["Human Approval"]
D --> S["Verifier"]
S --> T["Result Verification"]
D --> U["Observability"]
U --> V["Logs / Metrics / Trace"]
U --> W["Evaluation & Alerting"]
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:#DCEFE5,stroke:#4F8B70,color:#24563F
style E fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style J fill:#EAF3EC,stroke:#83A995,color:#315D49
style K fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style L fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style M fill:#EAF3EC,stroke:#83A995,color:#315D49
style N fill:#EAF3EC,stroke:#83A995,color:#315D49
style O fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style P fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style Q fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style R fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style S fill:#EAF3EC,stroke:#83A995,color:#315D49
style T fill:#E5F0E9,stroke:#83A995,color:#315D49
style U fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style V fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style W fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
3. Why Do We Need a Layered Architecture?
Mainly to prevent the model from becoming an uncontrollable central node in the system.
For example, the model may propose:
{
"tool": "update_order",
"arguments": {
"order_id": "ORD-1001",
"status": "cancelled"
}
}
But the fact that the model generates this request does not mean it has the authority to execute it.
Before actual execution, the system still needs to:
- Verify the identity of the current user.
- Check whether the user has permission to operate on orders.
- Verify whether the order status allows cancellation.
- Determine whether human approval is required.
- Check whether the parameters are valid.
- Execute the tool call.
- Verify whether the order status has actually changed.
This design separates the model's decision-making capability from the system's execution authority.
III. Designing the Agent's Task Execution Engine
The core of an Agent should not be merely a function that calls the model in a loop, but an execution engine capable of managing the task lifecycle.
1. Task Execution Lifecycle
Production-grade Agents usually need to decompose tasks into multiple explicit steps and maintain task state.
Typical task states include:
| Status | Meaning |
|---|---|
| PENDING | Awaiting execution |
| RUNNING | Executing |
| WAITING | Waiting for an external event or input |
| WAITING_APPROVAL | Waiting for manual approval |
| RETRYING | Retrying |
| SUCCEEDED | Execution succeeded |
| FAILED | Execution failed |
| CANCELLED | Cancelled by user |
| NEEDS_REVIEW | Requires manual handling |
Task state should not be stored only in the model context; it should be maintained by an independent state management module.
2. Task Execution Flow
flowchart TD
A["Receive user task"] --> B["Identity and permission check"]
B --> C["Create task record"]
C --> D["Task planning"]
D --> E["Save initial Checkpoint"]
E --> F["Execute current step"]
F --> G["Call model or tool"]
G --> H["Validate execution result"]
H --> I{"Current step succeeded?"}
I -->|"Yes"| J["Update task status"]
J --> K{"Are there unfinished steps?"}
K -->|"Yes"| L["Save Checkpoint"]
L --> F
K -->|"No"| M["Validate final result"]
M --> N["Save final state"]
N --> O["Return task result"]
I -->|"No"| P["Classify and handle exceptions"]
P --> Q{"Is recovery allowed?"}
Q -->|"Yes"| R["Retry or replan"]
R --> L
Q -->|"No"| S["Mark as failed or require manual intervention"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style D fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#EAF3EC,stroke:#83A995,color:#315D49
style K fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style L fill:#EAF3EC,stroke:#83A995,color:#315D49
style M fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style N fill:#E5F0E9,stroke:#83A995,color:#315D49
style O fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style P fill:#F7EAE7,stroke:#C98D7C,color:#754438
style Q fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style R fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style S fill:#F7EAE7,stroke:#C98D7C,color:#754438
3. Data Structures for Tasks and Steps
It is recommended to store tasks, steps, and execution records separately.
Task-level data:
{
"task_id": "task_1001",
"user_id": "user_001",
"status": "RUNNING",
"goal": "生成本季度销售分析报告",
"current_step": "step_003",
"created_at": "2026-11-21T10:00:00Z",
"deadline": "2026-11-21T10:30:00Z",
"attempts": 1,
"version": 3
}
Step-level data:
{
"step_id": "step_003",
"task_id": "task_1001",
"name": "calculate_sales_growth",
"status": "PENDING",
"dependencies": [
"step_001",
"step_002"
],
"idempotency_key": "task_1001_step_003",
"attempts": 0,
"result_ref": null
}
Managing tasks and steps separately makes it easier to support resuming from checkpoints, concurrent scheduling, step retries, and failure diagnosis.
IV. Building a Reliable Model Invocation Layer
Model services are an important dependency for agents, but you cannot assume that model APIs will always be available, nor that model outputs will always meet expectations.
1. Model Gateway
Do not let business agents depend directly on a single model vendor.
A unified model gateway should be established to hide the interface differences between different models.
Core functions:
- Unified request and response formats.
- Model selection and routing.
- Model version management.
- Token quotas.
- Request timeouts.
- Rate limiting and retries.
- Fallback model switching.
- Invocation cost statistics.
- Prompt version management.
For example:
class ModelGateway:
def __init__(self, providers, router):
self.providers = providers
self.router = router
async def generate(self, request):
model_name = self.router.select(request)
provider = self.providers[model_name]
return await provider.generate(
messages=request["messages"],
temperature=request.get("temperature", 0.2),
max_tokens=request.get("max_tokens", 2000),
)
Here, providers and router are the model adapters and routing strategies injected by the application layer; the specific model SDKs are implemented by the adapters.
2. Model Routing Strategy
Different tasks do not necessarily need the same model.
| Task Type | Model Selection Considerations |
|---|---|
| Intent classification | Low latency, low cost |
| Simple information extraction | Structured output capability |
| Complex task planning | Reasoning capability |
| Long-document analysis | Context window |
| Code generation | Coding capability |
| Result validation | Stability and factual judgment capability |
Routing can be based on task complexity:
flowchart TD
A["Receive model request"] --> B["Analyze task type"]
B --> C["Estimate task complexity"]
C --> D{"Complexity level"}
D -->|"Low"| E["Lightweight model"]
D -->|"Medium"| F["General-purpose model"]
D -->|"High"| G["High-capability model"]
E --> H["Unified response format"]
F --> H
G --> H
H --> I["Output model 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:#F7F4E8,stroke:#C7B77A,color:#675A2D
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
3. Structured Output and Result Validation
Production-grade agents should not rely entirely on free-form text generated by the model.
For content that needs to be consumed by programs, use JSON Schema or other structured output constraints.
For example:
{
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["search", "analyze", "finish"]
},
"reason": {
"type": "string"
},
"tool_name": {
"type": ["string", "null"]
},
"arguments": {
"type": "object"
}
},
"required": [
"action",
"reason",
"tool_name",
"arguments"
],
"additionalProperties": false
}
It is important to note that structured output only guarantees that the result conforms to the specified format; it does not mean the content is correct, nor does it mean the invocation behavior is safe.
Therefore, business semantic validation, permission validation, and argument range checks are also required.
V. Building a Secure Tool Execution System
Tools are the entry point through which agents connect to the real world, and they are also the part of production systems most likely to cause side effects.
The tool execution layer must not simply pass model-generated function calls directly to business systems for execution.
1. Tool Registry
All tools should be registered in a unified manner and include clear metadata.
For example:
tool_definition = {
"name": "query_sales",
"description": "查询授权范围内的销售数据",
"input_schema": {
"type": "object",
"properties": {
"start_date": {"type": "string"},
"end_date": {"type": "string"}
},
"required": ["start_date", "end_date"],
"additionalProperties": False
},
"risk_level": "low",
"required_permissions": [
"sales:read"
],
"timeout_seconds": 10,
"idempotent": True
}
The registry needs to manage:
- Tool name.
- Tool description.
- Parameter Schema.
- Tool version.
- Permission requirements.
- Risk level.
- Timeout settings.
- Idempotency.
- Invocation quota.
- Tool owner.
2. Secure Execution Flow for Tool Invocations
flowchart TD
A["Model generates tool call"] --> B["Tool name check"]
B --> C["Parameter Schema validation"]
C --> D["User identity authentication"]
D --> E["Permission policy check"]
E --> F{"Is execution allowed?"}
F -->|"No"| G["Deny call and log audit"]
F -->|"Yes"| H["Risk level check"]
H --> I{"Is approval required?"}
I -->|"Yes"| J["Wait for manual approval"]
I -->|"No"| K["Execute tool"]
J --> L{"Is approval passed?"}
L -->|"No"| G
L -->|"Yes"| K
K --> M["Result validation"]
M --> N["Log execution"]
N --> O["Return structured result"]
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:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style G fill:#F7EAE7,stroke:#C98D7C,color:#754438
style H fill:#EAF3EC,stroke:#83A995,color:#315D49
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style K fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style L fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style M fill:#EAF3EC,stroke:#83A995,color:#315D49
style N fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style O fill:#E5F0E9,stroke:#83A995,color:#315D49
3. Permission Control
Production-grade Agents should follow the principle of least privilege.
For example:
| Agent Type | Allowed Permissions | Prohibited Permissions |
|---|---|---|
| Query Agent | Read authorized data | Modify and delete data |
| Reporting Agent | Read data, create reports | Modify business records |
| Operations Agent | View monitoring, perform authorized operations | Unrestricted access to production hosts |
| Order Agent | Query orders, request cancellation | Direct refunds without approval |
Permission decisions should be based on the actual user identity, task context, resource scope, and current policy, rather than solely on the Agent's role name.
4. Defending Against Prompt Injection
Agents may read content from web pages, emails, documents, and third-party tools.
Such content may contain malicious instructions, for example, requiring the Agent to leak secrets, ignore existing constraints, or call unauthorized tools.
Therefore, it is necessary to:
- Treat external content as untrusted data.
- Distinguish system instructions from tool-returned content.
- Do not allow tool output to modify system permissions.
- Perform Schema validation on tool parameters.
- Use a tool allowlist.
- Require independent authorization for sensitive operations.
- Restrict network egress and file access.
- Set up manual approval for high-risk operations.
The key principle is: the model can understand untrusted content, but it must not automatically gain permission to execute instructions merely because those instructions appear in the content.
VI. Implementing State Persistence and Long-Term Task Management
Production-grade Agents often need to execute tasks that outlast a single model request lifecycle.
For example:
- Long-running research tasks.
- Multi-stage data analysis.
- Automated code repair.
- Cross-system business processing.
- Workflows that wait for manual approval.
If task state is stored only in process memory, execution progress may be lost after a service restart.
1. State and Checkpoint
State is the current task state of the Agent.
A Checkpoint is a persisted snapshot at a point in time, used to restore a task.
A task state can contain:
{
"task_id": "task_1001",
"goal": "完成销售分析报告",
"status": "RUNNING",
"current_step": "generate_report",
"completed_steps": [
"query_sales",
"calculate_growth"
],
"pending_actions": [],
"artifacts": [
"sales_analysis.json"
],
"retry_count": 1,
"version": 5
}
A Checkpoint needs to persist:
- Current task status.
- Completed steps.
- Current execution position.
- Pending actions.
- Tool execution result references.
- Retry count.
- State version.
- Creation time.
2. Interruption and Recovery
flowchart TD
A["Agent executes task"] --> B["Save Checkpoint periodically"]
B --> C["Execute next step"]
C --> D{"Is the task interrupted?"}
D -->|"No"| E["Continue execution"]
E --> B
D -->|"Yes"| F["Save interrupted state"]
F --> G["Service recovery or task rescheduling"]
G --> H["Read latest Checkpoint"]
H --> I["Validate state version"]
I --> J["Verify external side effects"]
J --> K["Resume unfinished steps"]
K --> B
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:#F7F4E8,stroke:#C7B77A,color:#675A2D
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F7EAE7,stroke:#C98D7C,color:#754438
style G fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style K fill:#E5F0E9,stroke:#83A995,color:#315D49
Special care is required: recovering a task cannot simply re-execute all tool calls.
If the previous execution already successfully created the order, but the Agent did not receive the response, blindly retrying may create duplicate orders.
Before recovery, the actual state of the external system must be verified first.
3. Idempotency and Duplicate Execution Control
It is recommended to generate an idempotency key for every tool call with side effects.
For example:
idempotency_key = (
f"{task_id}:{step_id}:{operation}"
)
During tool execution, check whether the operation has already been processed.
async def execute_once(
idempotency_key,
operation,
idempotency_store,
):
existing = await idempotency_store.get(
idempotency_key
)
if existing is not None:
return existing
result = await operation()
await idempotency_store.save(
idempotency_key,
result
)
return result
This is a conceptual example. In production, querying, executing, and recording operation results need to be made atomic through database unique constraints, transactions, Inbox/Outbox, or idempotent interfaces of external systems; they cannot rely solely on the check-then-write code above.
7. Building Exception Handling and Self-Recovery Mechanisms
In production environments, models, networks, tools, and business systems may all fail.
If every failure immediately terminates the task, it will be difficult for the Agent to run stably.
1. Exception Classification
| Exception Type | Example | Recommended Handling |
|---|---|---|
| Transient exception | Network timeout, temporary 5xx | Limited retries |
| Rate-limiting exception | HTTP 429 | Back off according to the rate-limiting policy |
| Parameter exception | Invalid parameter format | Correct the parameters or terminate |
| Permission exception | HTTP 403 | Refuse execution |
| Business exception | Order status does not allow modification | Return a business error |
| State exception | Checkpoint conflict | Validate and recover |
| Environment exception | Service unavailable | Pause, degrade, or switch |
| Uncertain side effects | Tool timed out but the operation may have completed | Query external state first |
2. Retry Strategy
It is recommended to use exponential backoff and random jitter.
For example:
import asyncio
import random
async def retry_call(
operation,
max_attempts=3,
base_delay=1.0,
max_delay=10.0,
):
for attempt in range(max_attempts):
try:
return await operation()
except (TimeoutError, ConnectionError):
if attempt == max_attempts - 1:
raise
delay = min(
max_delay,
base_delay * (2 ** attempt)
)
delay += random.uniform(0, 0.5)
await asyncio.sleep(delay)
This example only handles specified transient exceptions. Actual deployments also need to add an overall Deadline, cancellation propagation, server-side Retry-After, a retry budget, and classification of retryable errors.
For non-idempotent operations, do not simply call again just because of a timeout. You should first query the external system to confirm whether the previous operation has already taken effect.
3. Self-Healing and Replanning
When a tool returns a recoverable error, the Agent can adjust the execution plan based on the error information.
For example:
{
"ok": false,
"error_code": "INVALID_DATE_RANGE",
"retryable": false,
"message": "结束日期早于开始日期",
"details": {
"start_date": "2026-11-21",
"end_date": "2026-11-01"
}
}
The Agent can identify parameter issues and attempt to correct them, but it cannot reflect or re-execute indefinitely.
It is recommended to set:
- Maximum number of retries.
- Maximum number of replans.
- Overall task Deadline.
- Token budget.
- Tool-call budget.
- Maximum execution steps.
- Human-intervention threshold.
4. Circuit Breaking and Degradation
When an external service keeps failing, continuing to send requests may worsen the failure.
A circuit breaker can be introduced:
stateDiagram-v2
[*] --> Closed
Closed --> Open: consecutive failures reach threshold
Open --> HalfOpen: cooldown ends
HalfOpen --> Closed: probe succeeds
HalfOpen --> Open: probe fails
In real systems, the circuit breaker also needs to consider concurrent requests, the number of probes in the half-open state, and the recovery window.
Available degradation methods include:
- Switch to a backup model.
- Use cached results.
- Switch to read-only mode.
- Return partial results.
- Pause non-critical features.
- Hand the task over to a human.
VIII. Building a Memory and Knowledge Management System
An Agent's memory is not simply about saving all historical conversations; it needs to distinguish between different types of data and clearly define their lifecycles.
1. Short-Term Memory
Short-term memory mainly serves the current session or task.
Common contents:
- Current user request.
- Recent conversation records.
- Current task plan.
- Current step results.
- Temporary context.
- Unfinished subtasks.
Short-term memory usually needs to account for context window limits and information compression.
2. Long-Term Memory
Long-term memory stores information that is valuable across tasks.
For example:
- Preferences the user has explicitly authorized to be saved.
- Stable business rules.
- Historical task summaries.
- Confirmed knowledge.
- Long-term work background.
Long-term memory should have:
- Source markers.
- Timestamp information.
- Credibility.
- Access permissions.
- Update strategy.
- Expiration mechanism.
Speculation generated by the model must not be stored long-term as reliable fact.
3. RAG Knowledge Retrieval
For enterprise knowledge-oriented agents, RAG should be used to retrieve external knowledge rather than putting all documents directly into the prompt.
Typical flow:
flowchart TD
A["User question"] --> B["Query understanding"]
B --> C["Query rewriting"]
C --> D["Vector retrieval"]
C --> E["Keyword retrieval"]
D --> F["Result fusion"]
E --> F
F --> G["Permission filtering"]
G --> H["Reranking"]
H --> I["Build context"]
I --> J["Model generates answer"]
J --> K["Citation and fact verification"]
K --> L["Return 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:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style H fill:#EAF3EC,stroke:#83A995,color:#315D49
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style K fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style L fill:#E5F0E9,stroke:#83A995,color:#315D49
4. Governance of Memory and Knowledge
The following problems need to be avoided:
- Memory being mixed up between different users.
- Expired information overwriting new information.
- Untrusted documents contaminating long-term memory.
- Sensitive information being written into unprotected vector stores.
- Retrieval results bypassing the original data permissions.
- Unbounded memory growth degrading retrieval quality.