opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Add context control for subagents

Open gunnarnordqvist opened this issue 2 months ago • 1 comments

Summary

  • Add granular context control for subagents to optimize performance with small local models
  • Enable 90-99% token reduction and 5-10× faster inference for lightweight models

Implementation

This PR introduces native context filtering for subagents, allowing fine-grained control over what parent session context is passed to subagents.

Key Features

Context Modes:

  • none (default) - No parent context, maintains current behavior
  • summary - Compact session summary (~100-500 tokens)
  • filtered - Selective inclusion by message type and tool results
  • full - All context with configurable limits

Configuration:

context:
  mode: filtered
  maxTokens: 2000
  includeToolResults: ["read", "edit"]
  includeMessageTypes: ["user"]

Performance Impact

Before: 45,000 tokens → 8 seconds (llama3.2:1b)
After: 200 tokens → 1 second (99% reduction, 8× faster)

Changes

  • New files:

    • packages/opencode/src/session/context-filter.ts - Core filtering logic (246 lines)
    • packages/opencode/src/session/context-filter.test.ts - Comprehensive tests (237 lines)
    • .opencode/agent/ - Example agent configurations
  • Modified files:

    • packages/opencode/src/config/config.ts - Context filter schema
    • packages/opencode/src/agent/agent.ts - Agent context support
    • packages/opencode/src/tool/task.ts - Apply filtering before subagent invocation
    • AGENTS.md - Documentation

Backward Compatibility

  • ✅ Fully backward compatible
  • ✅ Default mode is none (current behavior)
  • ✅ Optional feature, no breaking changes
  • ✅ All existing tests passing

Testing

cd packages/opencode
bun test src/session/context-filter.test.ts

Test Results: ✅ 11 tests passing, 32 assertions

Addresses https://github.com/sst/opencode/issues/4096

gunnarnordqvist avatar Nov 09 '25 10:11 gunnarnordqvist

https://github.com/sst/opencode/issues/4096

gunnarnordqvist avatar Nov 09 '25 10:11 gunnarnordqvist