Skip to main content

Memory System

Qwen Code’s memory system allows the AI to remember important facts, preferences, and context across sessions, providing personalized and context-aware assistance.

Overview

The memory system consists of:
  • Global Memory: User-level facts shared across all projects (~/.qwen/QWEN.md)
  • Project Memory: Project-specific context (.qwen/QWEN.md or QWEN.md in project root)
  • Hierarchical Loading: Automatic discovery from workspace root to current directory
  • Import System: Include external files and organize memory content

Memory Files

File Locations

From packages/core/src/tools/memoryTool.ts:77:
Global memory:
Project memory:

File Format

Memory files are Markdown documents with optional structure:

Memory Tool

The save_memory tool allows Qwen Code to store facts for future reference.

Tool Schema

From memoryTool.ts:31:

Usage Examples

Save to Global Memory

This appends to ~/.qwen/QWEN.md:

Save to Project Memory

This appends to project’s QWEN.md:

Interactive Scope Selection

If scope is omitted, user is prompted to choose:
User sees:

Tool Behavior

From memoryTool.ts:165:
Key behaviors:
  • Creates ## Qwen Added Memories section if it doesn’t exist
  • Appends new facts as bullet points
  • Strips leading dashes from fact text
  • Maintains proper spacing

Hierarchical Memory Loading

Qwen Code automatically discovers and loads memory files in a hierarchical manner.

Discovery Process

From packages/core/src/utils/memoryDiscovery.ts:24:

Load Order

From memoryDiscovery.ts:68:
Memory files are loaded in this order:
  1. Global memory (~/.qwen/QWEN.md)
  2. Project root (found via .git directory)
  3. Intermediate directories (between project root and current directory)
  4. Current directory
  5. Extension context files (if any)
Example for /home/user/projects/myapp/src/components/:

Content Concatenation

From memoryDiscovery.ts:282:
Final memory content:

Import System

Memory files support importing other files.

Import Syntax

From packages/core/src/utils/memoryImportProcessor.ts:

Import Formats

Tree Format (default):
Flat Format:
Configuration:

Recursive Imports

Imports can be nested:

Configuration

Custom Filenames

From memoryTool.ts:90:
Usage:

Folder Trust

Memory loading respects folder trust settings:
When folderTrust is false, only global memory and explicitly included directories are loaded.

Best Practices

When to Use Global vs Project Memory

Global Memory (~/.qwen/QWEN.md):
  • Personal preferences and coding style
  • Tool configurations
  • Common patterns across all projects
  • Credential locations (not the credentials themselves!)
Example:
Project Memory (project’s QWEN.md):
  • Project architecture and structure
  • Technology stack and versions
  • Build and deployment processes
  • Team conventions
  • API endpoints and schemas
Example:

Memory File Organization

Structure memory files for clarity:

Import Organization

Use imports to keep files manageable:
This keeps the main file clean while providing comprehensive context.

Avoid Sensitive Information

Never store sensitive data in memory files: Bad:
Good:

Advanced Usage

Programmatic Memory Access

Memory in Resumable Sessions

Memory content is included in session checkpoints:

Extension Context Files

Extensions can provide additional context:

Troubleshooting

Memory Not Loading

Problem: Qwen Code doesn’t seem to remember context. Check:

Import Loops

Problem: Circular imports cause issues. Solution: Imports track visited files to prevent loops:
Avoid circular references:

Large Memory Files

Problem: Memory files too large, wasting tokens. Solution:
  1. Use imports to split content
  2. Keep only essential information
  3. Archive outdated context

Permission Issues

Problem: Cannot write to global memory file. Solution:

Source Code References

  • Memory tool: packages/core/src/tools/memoryTool.ts
  • Memory discovery: packages/core/src/utils/memoryDiscovery.ts
  • Import processor: packages/core/src/utils/memoryImportProcessor.ts
  • Configuration: packages/core/src/config/config.ts:52