OpenClaw

OpenClaw Sessions & Memory👨‍💻

Sessions and memory are how OpenClaw maintains conversation context across messages. A session is automatically created when a user starts a conversation, tracking message history, the active agent, and metadata. Memory goes deeper -- it is the persistent knowledge an agent retains across sessions, stored in MEMORY.md and managed through configurable compaction and flushing strategies. Understanding the difference between session-scoped context and persistent memory is essential for building agents that feel coherent over time.

Key Takeaways

  • 1Sessions are scoped to a user-channel-agent combination. If the same user talks to the same agent on WhatsApp and Telegram, they get two separate sessions. This prevents conversations from mixing across platforms.
  • 2Session state includes the full message history, metadata (timestamps, channel info), and any tool call results. It persists as long as the session is active and is stored locally in the agent's sessions directory.
  • 3MEMORY.md is the agent's persistent knowledge store. Unlike session history which resets, MEMORY.md content persists across sessions and is injected into every conversation. Use it for facts the agent should always know.
  • 4Context compaction automatically summarizes older messages when the context window approaches its limit. Instead of dropping old messages entirely, compaction creates a summary that preserves key information while freeing token budget.
  • 5The pre-compaction memory flush writes important facts from the conversation into MEMORY.md before they are compacted away. This ensures that valuable information discovered during a conversation is not lost when older messages are summarized.
  • 6Memory can be enabled or disabled per agent. Stateless agents (DevOps commands, alerts) should disable memory. Conversational agents (assistants, support) benefit from memory enabled with a reasonable maxMessages limit.

Master openclaw sessions & memory

Take the Introduction to OpenClaw course with hands-on lessons and challenges.

Examples

Agent memory configuration

json

The assistant agent keeps 100 messages of context per session, allowing it to reference earlier parts of the conversation. The alerter agent has memory disabled because each alert is independent -- there is no value in remembering previous alerts during a new one.

MEMORY.md structure for persistent knowledge

markdown

MEMORY.md lives in the agent's workspace directory. Its contents are injected into every conversation, so the agent always knows the user's preferences and project context. Keep it concise -- every line costs tokens on every message. Organize by topic and include dates for time-sensitive decisions.

Session management CLI commands

bash

Sessions can be inspected and managed via the CLI. The list command shows all active sessions for an agent. The view command shows the full message history. Export is useful for debugging or creating training data from real conversations.

Session scoping rules illustrated

bash

Session scoping is strict: user + channel + agent = unique session. This isolation is a feature, not a limitation. It prevents accidental context leakage between channels and ensures each agent only sees conversations meant for it.

Context compaction and memory flush configuration

json

When the context reaches 80% of the token limit (threshold: 0.8), compaction kicks in. It summarizes older messages while preserving the 20 most recent ones (preserveRecent). Before compaction runs, the flush trigger writes important facts from the about-to-be-compacted messages into MEMORY.md, ensuring key information survives the summarization.

Common Mistakes

Mistake:

Assuming sessions are shared across channels -- expecting a WhatsApp conversation to be visible when the same user messages on Discord

Fix:

Sessions are scoped to user + channel + agent. If a customer starts on WhatsApp and switches to Discord, they get a new session. Design your support workflow to account for this.

Mistake:

Filling MEMORY.md with low-value information like greetings or casual conversation details

Fix:

MEMORY.md content is injected into every conversation, costing tokens on every message. Only store high-value persistent facts: user preferences, project context, key decisions. Treat it like expensive real estate.

Mistake:

Setting maxMessages too high without compaction, causing the context window to overflow and older messages to be silently dropped

Fix:

Enable context compaction for agents with high maxMessages. Compaction summarizes older messages instead of dropping them, preserving key information while keeping the context within limits.

Mistake:

Disabling memory for conversational agents, causing them to forget the entire conversation after each message

Fix:

Memory should be enabled for any agent that has multi-turn conversations. Only disable it for stateless agents that handle one-off commands (alerts, deployments, health checks).

Best Practices

  • Start with maxMessages: 30 for most agents and increase only when you see the agent losing important context. Higher values cost more tokens per message.
  • Keep MEMORY.md concise and organized by topic. Use markdown headers and bullet points. Review and prune it periodically to remove outdated information.
  • Enable context compaction for any agent that might have long conversations. It is much better to summarize old messages than to silently drop them.
  • Enable pre-compaction memory flush for agents that discover important facts during conversations (preferences, decisions, corrections). This ensures those facts persist in MEMORY.md.
  • Use `openclaw sessions list` regularly to monitor active sessions. Long-running sessions with large message histories may indicate conversations that should be split or sessions that should be ended.
  • Back up MEMORY.md alongside your gateway configuration. It contains irreplaceable knowledge that the agent has accumulated over time.

Summary

Sessions track conversation state scoped to user + channel + agent, while MEMORY.md provides persistent knowledge that survives across sessions. Configure maxMessages based on how much context the agent needs, enable compaction to handle long conversations gracefully, and use pre-compaction flush to preserve important facts. Keep MEMORY.md concise with high-value information only. Disable memory for stateless agents and enable it with compaction for conversational ones.

Practice OpenClaw with hands-on challenges

Learn openclaw sessions & memory hands-on in your IDE

Interactive lessons and challenges on Stanza, practice in VS Code, Cursor, or the web.

Related Concepts

Related Cheatsheets

Master OpenClaw with Stanza

Interactive lessons and challenges, right in your code editor.

Check the free courses. No credit card.