intermediate45 minLesson 5 of 6

Memory and Context Management

Manage memory and context in OpenCode sessions. Learn how to maintain state across conversations, use memory files, and optimize context for better results.

Memory and Context Management

Understanding Context

Context is the information the AI remembers during a session:

Context TypeDescriptionPersistence
ConversationChat historySession only
File ContentRead filesUntil cleared
ProjectProject structureSession only
Memory FilesPersistent storageAcross sessions

Conversation Memory

View History

> /history

Save Session

> /save project-setup

Load Session

> /load project-setup

Clear Context

> /clear

Memory Files

Memory files persist information across sessions.

Project Memory

Create .opencode/memory/project.md:

markdown
# Project Memory ## Architecture - Frontend: React with TypeScript - Backend: Node.js with Express - Database: PostgreSQL ## Conventions - Use camelCase for variables - Use PascalCase for components - All API endpoints prefixed with /api/ ## Decisions - 2024-01-15: Chose PostgreSQL over MongoDB for ACID compliance

Personal Memory

Create ~/.config/opencode/memory/personal.md:

markdown
# Personal Memory ## Preferences - Preferred model: gpt-4o - Coding style: Functional programming - Testing: Jest with 80% coverage minimum ## Shortcuts - Use /clear between topics - Always review AI suggestions before applying

Loading Memory

Automatic Loading

Configure auto-load in opencode.json:

json
{ "memory": { "autoLoad": [ ".opencode/memory/project.md", ".opencode/memory/conventions.md" ] } }

Manual Loading

> Load the project memory file at .opencode/memory/project.md

Context Optimization

When Context Gets Too Large

Symptoms:

  • Slow responses
  • AI forgets early instructions
  • Token limit warnings

Solutions:

  1. Clear and reload essentials:
> /clear > Load .opencode/memory/project.md > We're working on the authentication module
  1. Use subagents for isolated tasks:
> Have the test-agent write tests for the login function
  1. Summarize long conversations:
> Summarize what we've discussed so far into a memory file

Context Window Management

> /verbose Context: 15234 tokens (47% of 32k limit)

Best Practices:

SituationAction
Context > 70%Clear and reload essentials
Switching topicsClear conversation
Complex projectUse memory files
Multiple tasksUse subagents

Memory File Structure

Recommended Format

markdown
# [Memory Type] Memory ## [Section] - Key point - Key point ## [Section] - Key point

Naming Conventions

FilePurpose
project.mdProject architecture and decisions
conventions.mdCoding standards and patterns
decisions.mdArchitectural decision records
todo.mdCurrent tasks and priorities
learned.mdThings learned during sessions

Using Memory in Prompts

Reference Memory

> According to our project memory, what database are we using?

Update Memory

> Update the project memory to note we've switched from MongoDB to PostgreSQL

Create from Session

> Save our conversation about the API design to .opencode/memory/api-design.md

Best Practices

Memory Hygiene

PracticeBenefit
Regular updatesKeeps memory accurate
Clear sectionsEasy to find information
Date decisionsTrack evolution
Remove outdatedPrevents confusion

Context Management

PracticeBenefit
Clear between topicsPrevents cross-contamination
Load essentials onlyReduces token usage
Use subagentsIsolates complex tasks
Monitor context sizeAvoids limits

Practice Questions

Practice Question

Where should you store project-specific memory files?

Practice Question

What is the benefit of memory files over conversation history?

Practice Question

When should you clear the conversation context?

Practice Question

What should you do when context exceeds 70% of the limit?

Practice Question

How do you update memory files during a session?


Success

Key Takeaways

  • Memory files persist information across sessions, unlike conversation history
  • Store project memory in .opencode/memory/ within the project
  • Clear conversation when switching topics to prevent cross-contamination
  • Monitor context size and reload essentials when it exceeds 70%
  • Use descriptive sections in memory files for easy retrieval
  • Memory files should be dated and regularly updated
  • Subagents help isolate complex tasks from main context
Progress83%