0Pricing
AI Agents with LangChain & Autonomous Workflows · 课时

管理智能体状态与会话

实施有效方法,在生产环境中的多次交互和用户会话之间维护智能体状态

管理智能体状态与会话 是 CoddyKit 上的免费 AI Agents with LangChain & Autonomous Workflows 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents with LangChain & Autonomous Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Agent State Matters

When building AI agents for real users, especially in production, your agent needs to remember things. Imagine a chatbot that forgets everything you said after each message – it would be frustrating!

This is where agent state and session management come in. They allow your agent to maintain context and have meaningful, continuous conversations.

Defining Agent State

Agent 'state' refers to all the information an agent needs to remember about a specific interaction or user session. This can include:

  • Past messages in a conversation
  • User preferences or settings
  • Intermediate results from tool usage
  • Any data collected during an interaction

Essentially, it's the agent's short-term and long-term memory for a given user.

The Stateless Challenge

By default, many interactions with Large Language Models (LLMs) are stateless. This means each API call is independent; the LLM doesn't inherently remember previous queries or responses.

In a production environment with many users, if you don't manage state, every interaction starts fresh. This leads to repetitive questions and a poor user experience.

Identifying User Sessions

To manage state for multiple users concurrently, we assign a unique 'session ID' to each user's interaction. This ID acts as a key to retrieve and store their specific conversation history and data.

Here's how you might generate a simple session ID:

import uuid

def start_user_session():
    # Generate a unique session ID
    session_id = str(uuid.uuid4())
    print(f"New session started with ID: {session_id}")
    return session_id

if __name__ == "__main__":
    # In a real app, this ID would be tied to a user
    # and sent with each request.
    current_session_id = start_user_session()
    # Use current_session_id to store and retrieve state

Persistent Storage for State

For production applications, in-memory storage for state is insufficient. If your server restarts, all in-memory state is lost. You need persistent storage.

Common choices include:

  • Key-value stores: Redis, Memcached
  • Databases: PostgreSQL, MongoDB, DynamoDB
  • Cloud storage: S3 (for larger, less frequent state)

These solutions ensure state survives restarts and can be accessed across distributed services.

LangChain's Memory Abstraction

LangChain simplifies state management with its Memory modules. These modules abstract away the complexity of storing and retrieving conversation history.

The simplest is ConversationBufferMemory, which stores messages in-memory. Let's see how it keeps track of messages:

from langchain.memory import ConversationBufferMemory
from langchain_core.messages import HumanMessage, AIMessage

# Initialize in-memory conversation buffer
memory = ConversationBufferMemory(
    memory_key="chat_history", 
    return_messages=True
)

# Simulate adding messages to the memory
memory.chat_memory.add_user_message("Hello, who are you?")
memory.chat_memory.add_ai_message("I am an AI assistant.")

memory.chat_memory.add_user_message("What can you do?")
memory.chat_memory.add_ai_message("I can answer questions and help with tasks.")

# Retrieve the current conversation history
history = memory.load_memory_variables({})["chat_history"]
print("Current conversation history:")
for message in history:
    print(f"{message.type.capitalize()}: {message.content}")

Integrating External Memory

While ConversationBufferMemory is great for development, production requires external persistence. LangChain provides specialized memory classes to integrate with various backends, like Redis or databases.

You connect these external stores by passing a ChatMessageHistory object to the ConversationBufferMemory (or other memory types).

from langchain.memory import ConversationBufferMemory
from langchain_community.chat_message_histories import RedisChatMessageHistory
import os

# In a real application, you would configure Redis URL
# os.environ["REDIS_URL"] = "redis://localhost:6379/0"

# For this runnable example, we'll simulate the external history store
class SimulatedChatMessageHistory:
    def __init__(self, session_id):
        self.session_id = session_id
        self._messages = []
        print(f"\nSimulated history for session: {session_id}")

    def add_user_message(self, message):
        # In a real app, this would save to Redis/DB
        self._messages.append(message)
        print(f"[Simulated Save] User: {message}")

    def add_ai_message(self, message):
        # In a real app, this would save to Redis/DB
        self._messages.append(message)
        print(f"[Simulated Save] AI: {message}")

    @property
    def messages(self):
        # In a real app, this would load from Redis/DB
        return self._messages

# Create a unique session ID for a user
user_session_id = "user_prod_session_123"

# Initialize a simulated external history store for this session
simulated_history = SimulatedChatMessageHistory(session_id=user_session_id)

# Now, integrate this with LangChain's memory system
memory = ConversationBufferMemory(
    chat_memory=simulated_history,
    memory_key="history",
    return_messages=True
)

# Simulate adding messages through the LangChain memory
# LangChain handles calling add_user_message/add_ai_message on simulated_history
memory.save_context({"input": "Hi there!"}, {"output": "Hello! How can I assist you?"})
memory.save_context({"input": "Tell me about AI agents."}, {"output": "AI agents combine LLMs with tools to perform tasks."})

print("\n--- Messages retrieved from LangChain memory (via simulated external store) ---")
for msg in memory.chat_memory.messages:
    print(f"-> {msg.type.capitalize()}: {msg.content}")

Production Session Strategies

In production, your web framework (e.g., Flask, FastAPI, Node.js Express) will typically manage assigning and tracking session IDs for users. When a user interacts with your agent:

  1. The framework identifies the user's session ID.
  2. This ID is passed to your agent service.
  3. Your agent service uses the ID to load the correct conversation state from persistent storage.
  4. After the agent processes the request, the updated state is saved back to persistent storage using the same ID.

State Management Best Practices

To ensure robust and scalable state management in production:

  • Session Expiry: Implement mechanisms to automatically clear old or inactive sessions to save storage costs and protect privacy.
  • Concurrency: Design your system to handle multiple requests from the same user safely, preventing race conditions when updating state.
  • Security: Protect session IDs (e.g., use secure cookies) and encrypt sensitive data stored in your persistent memory.
  • Scalability: Choose a persistent store that can scale horizontally with your user base and offers low-latency access.

Check Your Understanding

Which of the following is the primary reason for using a persistent storage solution (like Redis or a database) for agent state in a production environment, rather than just in-memory storage?

Recap: State & Sessions

We've learned that managing agent state and user sessions is vital for building robust, conversational AI agents in production. Key takeaways include:

  • Stateless Nature: LLMs are stateless by default, requiring explicit state management.
  • Session IDs: Used to uniquely identify and manage individual user contexts.
  • Persistent Storage: Essential for saving state across server restarts and distributed systems (e.g., Redis, databases).
  • LangChain Memory: Provides powerful abstractions to integrate various memory backends with your agents.

Mastering state management is key to delivering seamless and intelligent agent experiences. Next, we'll explore scaling agent architectures!

常见问题解答

「管理智能体状态与会话」课时是免费的吗?

是的 — 「管理智能体状态与会话」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents with LangChain & Autonomous Workflows 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。

「管理智能体状态与会话」这节课中我会学到什么?

实施有效方法,在生产环境中的多次交互和用户会话之间维护智能体状态 你通过在浏览器中直接运行的动手代码来练习 AI Agents with LangChain & Autonomous Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Agents with LangChain & Autonomous Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Agents with LangChain & Autonomous Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「管理智能体状态与会话」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Agents with LangChain & Autonomous Workflows 课中编写并运行代码吗?

能。每节 AI Agents with LangChain & Autonomous Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 将智能体部署到云平台
  2. 管理智能体状态与会话
  3. 扩展智能体架构
  4. 速率限制与 API 配额管理
← 返回 AI Agents with LangChain & Autonomous Workflows