0Pricing
Prompt Engineering & LLM Optimization for Developers · บทเรียน

การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์

นำเทคนิคหน่วยความจำถาวรและการจัดการสถานะสำหรับเอเจนต์ LLM ไปใช้งาน เพื่อรองรับการสนทนาระยะยาวและลำดับงานซับซ้อน

การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์ เป็นบทเรียน Prompt Engineering & LLM Optimization for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Prompt Engineering & LLM Optimization for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Agents Need Memory

LLM agents are designed to perform complex tasks. But by default, LLMs are stateless! This means they don't "remember" past interactions.

For an agent to have a meaningful conversation or complete multi-step tasks, it needs a way to recall previous information. This is where memory comes in.

Short & Long-Term Memory

Agent memory can be categorized into two main types:

  • Short-Term Memory: This is like a human's working memory. It holds recent, relevant information for immediate use, often the raw conversation history.
  • Long-Term Memory: Stores information over extended periods, like past conversations or learned facts. It helps agents retain knowledge beyond the current interaction.

Conversational Buffer Memory

The simplest form of short-term memory for an agent is a conversational buffer. It stores the raw exchange of messages between the user and the agent.

When generating a new response, the agent is prompted with the entire buffer, allowing it to maintain context for the current turn.

Basic Chat Buffer Demo

Here's a simple Python example simulating a conversational buffer. Each new message is added to a list that represents the agent's memory.

class AgentMemory:
    def __init__(self):
        self.history = []

    def add_message(self, role, content):
        self.history.append(f"{role}: {content}")

    def get_history(self):
        return "\n".join(self.history)

# Simulate agent interaction
memory = AgentMemory()
memory.add_message("User", "Hi, what's the weather like?")
memory.add_message("Agent", "It's sunny today.")
memory.add_message("User", "What about tomorrow?")

print("Current conversation history:")
print(memory.get_history())

Summarizing for Long-Term

Raw conversation buffers grow quickly, hitting LLM context window limits and increasing costs. Summarization is a key technique for long-term memory.

Instead of sending the full history, we periodically summarize older parts of the conversation, keeping the key points while discarding verbose details.

Summarized Memory Concept

This example shows how a summary might be generated and used. In a real application, an LLM would create the summary, but here we simulate it.

class AgentMemory:
    def __init__(self):
        self.buffer = []
        self.summary = "No previous conversation."

    def add_message(self, role, content):
        self.buffer.append(f"{role}: {content}")
        # In a real app, trigger LLM summarization here
        if len(self.buffer) > 4: # Example threshold
            self.summarize_buffer()

    def summarize_buffer(self):
        # Simulate LLM summarizing the buffer
        old_messages = "\n".join(self.buffer[:-2])
        new_summary = f"Summary of past: {old_messages[:30]}..."
        self.summary = new_summary
        self.buffer = self.buffer[-2:] # Keep recent messages

    def get_full_context(self):
        return f"Previous summary: {self.summary}\n" + \
               "\n".join(self.buffer)

memory = AgentMemory()
memory.add_message("User", "Tell me about your capabilities.")
memory.add_message("Agent", "I can answer questions.")
memory.add_message("User", "Can you write code?")
memory.add_message("Agent", "Yes, in Python.")
memory.add_message("User", "What about Java?") # This triggers summary
memory.add_message("Agent", "I can try some Java.")

print("Agent's current context:")
print(memory.get_full_context())

Agent Task State

Beyond just remembering conversation, agents need to manage state to complete multi-step tasks. This means tracking progress, decisions made, and information collected.

Think of it like a checklist or a finite state machine for the agent's goal. For example, booking a flight requires knowing source, destination, and dates.

Flight Booking State Example

Consider an agent helping book a flight. Its internal state might include:

  • Status: "collecting_info", "searching_flights", "booking_confirmed"
  • Departure City: "London"
  • Destination City: "Paris"
  • Departure Date: "2024-12-25"
  • Number of Passengers: "1"

The agent updates this state as it gathers information from the user.

Memory & State Challenges

Managing agent memory and state introduces several key challenges:

  • Context Window Limits: How much information can the LLM process at once?
  • Cost: Longer contexts mean more tokens and higher API costs.
  • Retrieval: For very long-term memory (e.g., a knowledge base), how does the agent efficiently find the most relevant past information?
  • Consistency: Ensuring the agent's state accurately reflects the task's progress and collected data.

Memory Check

Let's check your understanding of agent memory and state management techniques.

Recap: Memory & State

In this lesson, we explored how memory and state are essential for LLM agents to engage in long-term conversations and complete complex tasks.

  • We covered short-term conversational buffers and long-term summarization techniques.
  • We also looked at how state management helps agents track progress through multi-step processes.

These techniques are fundamental for building truly capable and persistent AI agents.

คำถามที่พบบ่อย

บทเรียน “การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Prompt Engineering & LLM Optimization for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์”

นำเทคนิคหน่วยความจำถาวรและการจัดการสถานะสำหรับเอเจนต์ LLM ไปใช้งาน เพื่อรองรับการสนทนาระยะยาวและลำดับงานซับซ้อน คุณปฏิบัติ Prompt Engineering & LLM Optimization for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Prompt Engineering & LLM Optimization for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Prompt Engineering & LLM Optimization for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Prompt Engineering & LLM Optimization for Developers นี้ได้ไหม

ได้ บทเรียน Prompt Engineering & LLM Optimization for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การออกแบบระบบหลายเอเจนต์
  2. การจัดการหน่วยความจำและสถานะสำหรับเอเจนต์
  3. การทำงานอัตโนมัติของกระบวนการ
  4. การไตร่ตรองและวงจรแก้ไขตนเองของเอเจนต์
← กลับไปที่ Prompt Engineering & LLM Optimization for Developers