What it is

Context is everything the language model can "see" when generating a response. It is the working memory of an AI session — the system prompt, your conversation, tool outputs, file contents, and project-specific rules all packed into a single token budget.

What it is in real OpenCode terms

In OpenCode, context is the combined set of information fed to the LLM on each turn. It includes:

Context is session-scoped. It is not shared between different sessions. Each session starts with its own clean context that grows as you interact.

Why it exists

Anatomy / main elements

Context has a finite size (measured in tokens). Here is roughly how it fills up:

┌─────────────────────────────────┐
│  System Prompt                  │  ← OpenCode's base instructions
├─────────────────────────────────┤
│  Rules / Instructions           │  ← AGENTS.md, .opencode/rules/
├─────────────────────────────────┤
│  Conversation History          │  ← Messages from this session
├─────────────────────────────────┤
│  Tool Outputs                  │  ← File contents, bash output, etc.
├─────────────────────────────────┤
│  MCP Tool Descriptions         │  ← Schemas from connected servers
├─────────────────────────────────┤
│  Skill Content                 │  ← Loaded on-demand by skills
└─────────────────────────────────┘

When this fills up, compaction kicks in to summarize and reduce the token count while preserving important state.

Where it lives

Context is assembled at runtime for each session. The pieces come from:

How to create one

You do not "create" context manually. OpenCode assembles it. But you can influence what goes into it:

# Add project instructions
cat > AGENTS.md <<'EOF'
# Project Rules
- Use TypeScript strict mode
- Prefer named exports over default exports
- Run `npm run lint` before committing
EOF

# Add more rule files
mkdir -p .opencode/rules
cat > .opencode/rules/testing.md <<'EOF'
# Testing Rules
- Write tests for all new functions
- Use vitest, not jest
- Test file goes next to source file with .test.ts suffix
EOF

# Point to additional context in opencode.json
cat > opencode.json <<'EOF'
{
  "instructions": ["./docs/ARCHITECTURE.md", "./docs/API.md"]
}
EOF

Minimal example

A project with well-structured context:

# Project structure
my-project/
  AGENTS.md                    # General agent instructions
  .opencode/
    rules/
      architecture.md          # Architecture decisions
      coding-style.md          # Code conventions
    plugins/
      lint-on-edit.ts          # Plugin with hooks
  opencode.json
    # { "instructions": ["./docs/CONTRIBUTING.md"] }

# When you start a session, context includes:
# - System prompt (built-in)
# - AGENTS.md content
# - All .opencode/rules/*.md files
# - docs/CONTRIBUTING.md content
# - Conversation history (grows as you chat)
# - Tool outputs (file reads, searches, etc.)

Related concepts

Common confusion

Is context the same as session? No. A session is the conversation container. Context is the data within it that the LLM sees. A session owns its context, but they are different concepts.
Why is my context getting huge? Tool outputs are the main culprit. Every file read, bash output, and search result gets added to context. Large files can consume thousands of tokens. OpenCode compacts context when it gets too full.
Can two sessions share context? No. Context is session-scoped. Each session starts fresh. If you need information from another session, you must re-provide it or use --continue to resume that session.
What is compaction? When context exceeds the LLM's token limit, OpenCode summarizes older messages into a compact summary. The session.compacted event fires, and the experimental.session.compacting hook lets you customize this process.