> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 什么是存储？

> 存储是一种将 Agent 会话和状态持久化到数据库或文件的方法。

使用**会话存储**将 Agent 会话和状态持久化到数据库或文件。

<Tip>
  **为什么我们需要会话存储？**

  Agents 是短暂的，内置的内存只在当前的执行周期内有效。

  在生产环境中，我们通过 API 提供（或触发）Agents，需要在多次请求中继续相同的会话。存储会将会话历史和状态持久化到数据库中，使我们能够接续之前的工作。

  存储还允许我们检查和评估 Agent 的会话，提取少样本示例，并构建内部监控工具。它让我们能够**查看数据**，这有助于我们构建更优秀的 Agents。
</Tip>

将存储添加到 Agent、Team 或 Workflow 中，只需提供一个 `Storage` 驱动，Agno 会处理其余部分。您可以使用 Sqlite、Postgres、Mongo 或任何您想要的数据库。

这是一个演示跨执行周期持久化的简单示例：

```python storage.py theme={null}
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.storage.sqlite import SqliteStorage
from rich.pretty import pprint

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    # 固定 session id 以在执行周期中继续相同的会话
    session_id="fixed_id_for_demo",
    storage=SqliteStorage(table_name="agent_sessions", db_file="tmp/data.db"),
    add_history_to_messages=True,
    num_history_runs=3,
)
agent.print_response("我上一个问题是什么？")
agent.print_response("法国的首都是哪里？")
agent.print_response("我上一个问题是什么？")
pprint(agent.get_messages_for_session())
```

第一次运行此代码时，“我上一个问题是什么？”的答案将不可用。但再次运行，Agent 将能够正确回答。因为我们固定了 session id，Agent 每次运行脚本时都会从同一个会话继续。

## 存储的好处

存储通常是 Agent 工程中一个被低估的部分——但我们认为它是生产环境中 Agentic 应用的无名英雄。

在生产环境中，您需要存储来：

* 继续会话：检索会话历史并接续之前的工作。
* 获取会话列表：要继续之前的会话，您需要维护一个可用的会话列表。
* 在运行之间保存状态：将 Agent 的状态保存到数据库或文件以便以后检查。

但还有更多：

* 存储保存我们的 Agent 的会话数据以供检查和评估。
* 存储帮助我们提取少样本示例，这些示例可用于改进 Agent。
* 存储使我们能够构建内部监控工具和仪表板。

<Warning>
  存储是您的 Agentic 基础设施中至关重要的一部分，绝不应外包给第三方。您几乎应该始终为您的 Agent 使用自己的存储层。
</Warning>

## Agent 存储

在处理 Agents 时，存储允许用户接续之前的对话。每条消息以及 Agent 的响应都会被保存到您选择的数据库中。

这是一个向 Agent 添加存储的简单示例：

```python storage.py theme={null}
"""Run `pip install duckduckgo-search sqlalchemy openai` to install dependencies."""

from agno.agent import Agent
from agno.storage.sqlite import SqliteStorage
from agno.tools.duckduckgo import DuckDuckGoTools

agent = Agent(
    storage=SqliteStorage(
        table_name="agent_sessions", db_file="tmp/data.db", auto_upgrade_schema=True
    ),
    tools=[DuckDuckGoTools()],
    add_history_to_messages=True,
    add_datetime_to_instructions=True,
)
agent.print_response("加拿大有多少人？")
agent.print_response("他们的国歌是什么？")
agent.print_response("逐一列出我的消息")
```

## Team 存储

`Storage` 驱动程序也适用于 Teams，为多 Agent 协作系统提供持久化内存和状态管理。通过团队存储，您可以维护跨多个会话的对话历史、共享上下文和团队状态。

<Note>
  了解更多关于[Teams](/teams/) 及其存储能力，以构建具有持久化状态的强大多 Agent 系统。
</Note>

## Workflow 存储

Agno 中的存储系统也与工作流配合使用，实现了更复杂的多 Agent 系统和状态管理。这允许在工作流会话之间进行持久化对话和缓存结果。

<Note>
  了解更多关于使用存储与[Workflows](/workflows/) 来构建具有状态管理功能的强大多 Agent 系统。
</Note>

## 支持的存储后端

以下数据库被支持作为存储后端：

* [PostgreSQL](/storage/postgres)
* [Sqlite](/storage/sqlite)
* [SingleStore](/storage/singlestore)
* [DynamoDB](/storage/dynamodb)
* [MongoDB](/storage/mongodb)
* [YAML](/storage/yaml)
* [JSON](/storage/json)
* [Redis](/storage/redis)

查看每个存储的详细[示例](/examples/concepts/storage)
