Codex Brain — Persistent AI Memory & Context Management Skill
The external long-term memory engine of AI Codex. Solves context window limits, prevents conversational amnesia, snapshots critical engineering decisions, and enables high-density token compression.
Overview
AI models have finite context windows. In long-running projects, conversation history degrades, tokens run out, and critical decisions made yesterday are forgotten in a new chat today.
codex-brain provides externalized, persistent memory for AI sessions. It captures high-signal decision summaries, tracks uncompleted work, records environment gotchas, and stores compact memory checkpoints inside codex-drive/brains/. Any fresh chat session can read a brain checkpoint in < 1,000 tokens to instantly achieve 100% awareness of past engineering work.
When to Trigger
- User runs
/codex-brain or its subcommands:
/codex-brain save [optional-topic] — Snapshot current session memory before closing or when context is getting full.
/codex-brain load [optional-topic|latest] — Restore memory into a fresh chat session.
/codex-brain dense [on|off] — Toggle high-density "Caveman" token compression mode (saves up to 70% of tokens).
/codex-brain compact — Consolidate multiple session snapshots into the master knowledge base.
/codex-brain index — Scan repository conventions, architecture rules, and domain glossary.
- Starting a new chat session on an ongoing project (automatic context resumption via
/codex-start).
- When context window usage is high and important decisions must be saved before starting a new chat.
Memory Hierarchy
┌────────────────────────────────────────────────────────┐
│ 1. WORKING MEMORY (Active State & Immediate Tasks) │
│ What was just done, what is blocked, what is next. │
├────────────────────────────────────────────────────────┤
│ 2. EPISODIC MEMORY (Session Snapshots & Decisions) │
│ Why architectural option A was chosen over B. │
├────────────────────────────────────────────────────────┤
│ 3. SEMANTIC MEMORY (Consolidated Knowledge Base) │
│ Project conventions, domain glossary, quirks. │
└──────────────────────────┬─────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────┐
│ HIGH-DENSITY TOKEN COMPRESSION ENGINE ("Caveman") │
│ Strips conversational fluff, keeps 100% exact code. │
└────────────────────────────────────────────────────────┘
Sub-Commands & Workflows
1. Snapshot Session Memory: /codex-brain save [topic]
- Analyze the current conversation history.
- Extract:
- Core Objectives: What problem was addressed in this session.
- Decisions Made & Alternatives Rejected: Concrete rationale for architectural choices.
- Completed Tasks vs. Next Immediate Tasks: Exact continuation point for the next session.
- Gotchas & Quirks Learned: Discovered edge cases, environment settings, API peculiarities.
- Write timestamped checkpoint file to:
codex-drive/brains/YYYY-MM-DD-session-<slug>.brain.md
2. Restore Memory Context: /codex-brain load [topic|latest]
- Locate the latest or requested
.brain.md file in codex-drive/brains/.
- Parse the summary, active state, decisions, and remaining tasks.
- Output a concise resumption brief to the user:
"Context Restored from [YYYY-MM-DD-session-.brain.md]. Last worked on: [Objective]. Next task on deck: [Next Task]. Ready to proceed."
3. High-Density Token Mode: /codex-brain dense [on|off]
When dense mode is active (ideal for smaller context models like Claude 3.5 Haiku, Gemini Flash, or local Ollama):
- Strip Fluff: Eliminate all polite conversational padding, preambles, and repetitive disclaimers.
- Byte-Exact Code: Preserve exact code blocks, diffs, shell commands, and file paths without modification.
- Telegraphic Precision: Format reasoning as concise bullet points. Reduces token consumption by up to 70%.
4. Consolidate Memory: /codex-brain compact
- Read all historical
YYYY-MM-DD-session-*.brain.md files in codex-drive/brains/.
- Extract recurring rules, finalized decisions, and permanent patterns.
- Merge and update
codex-drive/brains/knowledge-base.brain.md.
- Archive or prune redundant intermediate session files to prevent memory sprawl.
5. Repository Context Indexing: /codex-brain index
- Scan workspace root, folder topology, package manifests, and configuration files.
- Catalog ubiquitous domain terms, models, schemas, and coding conventions.
- Update
codex-drive/brains/knowledge-base.brain.md.
Context Compression Protocol
When generating a session snapshot, follow the High-Density Signal Rule:
- Discard: Conversational pleasantries, intermediate tool call logs, repetitive code dumps, syntax trial-and-error.
- Preserve: Final decisions, rejected alternatives with reasons, file paths created/modified, failing tests that need fixing, exact continuation instructions.
- Target Density: Compress 40,000+ tokens of verbose conversation into ~600 to 1,000 tokens of structured markdown.
Memory File Specification (codex-drive/brains/)
All memory files generated by codex-brain MUST be Markdown (.md) files with exact date-time metadata.
Filename Formats:
- Session Snapshot:
codex-drive/brains/YYYY-MM-DD-session-<slug>.brain.md
- Master Knowledge Base:
codex-drive/brains/knowledge-base.brain.md
Standard Session Memory Template:
# [Session / Feature Name] Memory Checkpoint
> **Created At**: YYYY-MM-DD HH:MM:SS (Local Time)
> **Active Codex Edition**: [`skills/codex/<edition>/`](file:///...)
> **Memory Type**: SESSION_SNAPSHOT
> **Compression Ratio**: [e.g., ~42,000 tokens compressed into ~650 tokens]
> **Status**: READY_FOR_RESUMPTION | ARCHIVED
---
## 1. Session Objective & Scope
[Concise summary of what was tackled in this session]
## 2. Key Decisions & Rationale
- **Decision 1**: Chose PostgreSQL row-level locking (`FOR UPDATE`) instead of Redis distributed lock because the cluster is single-region and DB transaction atomicity is required.
- **Decision 2**: Rejected library X due to lack of Swift 6 strict concurrency / Sendable support.
## 3. Work Completed in This Session
- [x] Implemented domain aggregate `Order` in `src/domain/order.ts`
- [x] Added database migration `004_create_orders.sql`
- [x] Wrote unit test suite with 100% invariant coverage
## 4. Active Continuation Point (Next Session Resumes Here)
- [ ] Implement HTTP controller in `src/api/order-controller.ts`
- [ ] Wire dependency injection container in `src/server.ts`
- [ ] Run integration tests with Testcontainers (`npm run test:integration`)
## 5. Critical Gotchas & Environment Quirks Learned
- [WARNING] Webhook payload timestamps are emitted in milliseconds UTC, not seconds.
- [WARNING] Database pool max connections must not exceed 20 to avoid exhausting AWS RDS limits.
- [WARNING] Ensure `CancellationToken` is propagated in all async database queries.
Response Protocol
When codex-brain save completes:
- Provide a direct link to
[View Brain Memory Checkpoint](file:///.../codex-drive/brains/YYYY-MM-DD-session-<slug>.brain.md).
- Display a compact 3-bullet resumption summary (What was done, What decisions were locked in, What is next).
- Inform the user: "You can start a fresh chat anytime and run
/codex-brain load or /codex-start to instantly resume with full context."
1---2name: codex-brain3description: Manages long-term AI memory, overcomes context-window limits, snapshots chat session decisions, restores context into fresh sessions, supports high-density token-saving modes, and maintains living repository knowledge in codex-drive/brains/.4---56# Codex Brain — Persistent AI Memory & Context Management Skill78> The external long-term memory engine of AI Codex. Solves context window limits, prevents conversational amnesia, snapshots critical engineering decisions, and enables high-density token compression.910---1112## Overview1314AI models have finite context windows. In long-running projects, conversation history degrades, tokens run out, and critical decisions made yesterday are forgotten in a new chat today.1516`codex-brain` provides **externalized, persistent memory** for AI sessions. It captures high-signal decision summaries, tracks uncompleted work, records environment gotchas, and stores compact memory checkpoints inside `codex-drive/brains/`. Any fresh chat session can read a brain checkpoint in `< 1,000 tokens` to instantly achieve 100% awareness of past engineering work.1718---1920## When to Trigger2122- User runs `/codex-brain` or its subcommands:23 - `/codex-brain save [optional-topic]` — Snapshot current session memory before closing or when context is getting full.24 - `/codex-brain load [optional-topic|latest]` — Restore memory into a fresh chat session.25 - `/codex-brain dense [on|off]` — Toggle high-density "Caveman" token compression mode (saves up to 70% of tokens).26 - `/codex-brain compact` — Consolidate multiple session snapshots into the master knowledge base.27 - `/codex-brain index` — Scan repository conventions, architecture rules, and domain glossary.28- Starting a new chat session on an ongoing project (automatic context resumption via `/codex-start`).29- When context window usage is high and important decisions must be saved before starting a new chat.3031---3233## Memory Hierarchy3435```36 ┌────────────────────────────────────────────────────────┐37 │ 1. WORKING MEMORY (Active State & Immediate Tasks) │38 │ What was just done, what is blocked, what is next. │39 ├────────────────────────────────────────────────────────┤40 │ 2. EPISODIC MEMORY (Session Snapshots & Decisions) │41 │ Why architectural option A was chosen over B. │42 ├────────────────────────────────────────────────────────┤43 │ 3. SEMANTIC MEMORY (Consolidated Knowledge Base) │44 │ Project conventions, domain glossary, quirks. │45 └──────────────────────────┬─────────────────────────────┘46 │47 ▼48 ┌────────────────────────────────────────────────────────┐49 │ HIGH-DENSITY TOKEN COMPRESSION ENGINE ("Caveman") │50 │ Strips conversational fluff, keeps 100% exact code. │51 └────────────────────────────────────────────────────────┘52```5354---5556## Sub-Commands & Workflows5758### 1. Snapshot Session Memory: `/codex-brain save [topic]`591. Analyze the current conversation history.602. Extract:61 - **Core Objectives**: What problem was addressed in this session.62 - **Decisions Made & Alternatives Rejected**: Concrete rationale for architectural choices.63 - **Completed Tasks vs. Next Immediate Tasks**: Exact continuation point for the next session.64 - **Gotchas & Quirks Learned**: Discovered edge cases, environment settings, API peculiarities.653. Write timestamped checkpoint file to:66 `codex-drive/brains/YYYY-MM-DD-session-<slug>.brain.md`6768### 2. Restore Memory Context: `/codex-brain load [topic|latest]`691. Locate the latest or requested `.brain.md` file in `codex-drive/brains/`.702. Parse the summary, active state, decisions, and remaining tasks.713. Output a concise resumption brief to the user:72 > *"Context Restored from [YYYY-MM-DD-session-<slug>.brain.md]. Last worked on: [Objective]. Next task on deck: [Next Task]. Ready to proceed."*7374### 3. High-Density Token Mode: `/codex-brain dense [on|off]`75When `dense` mode is active (ideal for smaller context models like Claude 3.5 Haiku, Gemini Flash, or local Ollama):76- **Strip Fluff**: Eliminate all polite conversational padding, preambles, and repetitive disclaimers.77- **Byte-Exact Code**: Preserve exact code blocks, diffs, shell commands, and file paths without modification.78- **Telegraphic Precision**: Format reasoning as concise bullet points. Reduces token consumption by up to **70%**.7980### 4. Consolidate Memory: `/codex-brain compact`811. Read all historical `YYYY-MM-DD-session-*.brain.md` files in `codex-drive/brains/`.822. Extract recurring rules, finalized decisions, and permanent patterns.833. Merge and update `codex-drive/brains/knowledge-base.brain.md`.844. Archive or prune redundant intermediate session files to prevent memory sprawl.8586### 5. Repository Context Indexing: `/codex-brain index`871. Scan workspace root, folder topology, package manifests, and configuration files.882. Catalog ubiquitous domain terms, models, schemas, and coding conventions.893. Update `codex-drive/brains/knowledge-base.brain.md`.9091---9293## Context Compression Protocol9495When generating a session snapshot, follow the **High-Density Signal Rule**:96- **Discard**: Conversational pleasantries, intermediate tool call logs, repetitive code dumps, syntax trial-and-error.97- **Preserve**: Final decisions, rejected alternatives with reasons, file paths created/modified, failing tests that need fixing, exact continuation instructions.98- **Target Density**: Compress 40,000+ tokens of verbose conversation into **~600 to 1,000 tokens** of structured markdown.99100---101102## Memory File Specification (`codex-drive/brains/`)103104All memory files generated by `codex-brain` MUST be Markdown (`.md`) files with exact date-time metadata.105106### Filename Formats:107- Session Snapshot: `codex-drive/brains/YYYY-MM-DD-session-<slug>.brain.md`108- Master Knowledge Base: `codex-drive/brains/knowledge-base.brain.md`109110### Standard Session Memory Template:111112```markdown113# [Session / Feature Name] Memory Checkpoint114115> **Created At**: YYYY-MM-DD HH:MM:SS (Local Time)116> **Active Codex Edition**: [`skills/codex/<edition>/`](file:///...)117> **Memory Type**: SESSION_SNAPSHOT118> **Compression Ratio**: [e.g., ~42,000 tokens compressed into ~650 tokens]119> **Status**: READY_FOR_RESUMPTION | ARCHIVED120121---122123## 1. Session Objective & Scope124[Concise summary of what was tackled in this session]125126## 2. Key Decisions & Rationale127- **Decision 1**: Chose PostgreSQL row-level locking (`FOR UPDATE`) instead of Redis distributed lock because the cluster is single-region and DB transaction atomicity is required.128- **Decision 2**: Rejected library X due to lack of Swift 6 strict concurrency / Sendable support.129130## 3. Work Completed in This Session131- [x] Implemented domain aggregate `Order` in `src/domain/order.ts`132- [x] Added database migration `004_create_orders.sql`133- [x] Wrote unit test suite with 100% invariant coverage134135## 4. Active Continuation Point (Next Session Resumes Here)136- [ ] Implement HTTP controller in `src/api/order-controller.ts`137- [ ] Wire dependency injection container in `src/server.ts`138- [ ] Run integration tests with Testcontainers (`npm run test:integration`)139140## 5. Critical Gotchas & Environment Quirks Learned141- [WARNING] Webhook payload timestamps are emitted in milliseconds UTC, not seconds.142- [WARNING] Database pool max connections must not exceed 20 to avoid exhausting AWS RDS limits.143- [WARNING] Ensure `CancellationToken` is propagated in all async database queries.144```145146---147148## Response Protocol149150When `codex-brain save` completes:1511. Provide a direct link to `[View Brain Memory Checkpoint](file:///.../codex-drive/brains/YYYY-MM-DD-session-<slug>.brain.md)`.1522. Display a compact 3-bullet resumption summary (What was done, What decisions were locked in, What is next).1533. Inform the user: *"You can start a fresh chat anytime and run `/codex-brain load` or `/codex-start` to instantly resume with full context."*