Skip to content

Epic: Token Reduction for Command & Agent Files (~47-60% savings) #659

Closed

Description

@jflowers

Overview

Analysis of the .opencode/agents/ (12 files, ~118 KB) and .opencode/commands/ (11 files, ~318 KB) directories reveals significant token waste from cross-file duplication, procedural logic described as prose, inline data tables, and agent boilerplate. Estimated total savings: 36,000–46,000 tokens (47–60%) across ~77,000 current tokens.

Current State

Category Files Total Lines Est. Tokens
Commands 11 8,107 ~60,000
Agents 12 2,267 ~17,000
Total 23 10,374 ~77,000

Key Findings

  1. Cross-command duplication (~30%): Session-resume guards, mandatory gate patterns, dispatch protocol, GitHub review posting, model name resolution, and shell injection prevention are copy-pasted across 3-6 files each.
  2. Procedural logic as prose (~20%): 115 deterministic procedures (~24,400 tokens) across 6 major command files — 62 invoke external CLIs, 27 do pure data transformation, 26 read config files. All are 100% deterministic with no creative judgment needed.
  3. Divisor agent boilerplate (~35% of agent tokens): All 5 Divisor agents share identical Prior Learnings, Source Documents, Output Format, Decision Criteria, and dual-mode explanation blocks.
  4. Inline data (~5%): Lookup tables, verdict mappings, path classifications, and schemas embedded as prose instead of deterministic lookups.
  5. Embedded subagent prompts: uf.review-pr contains a ~500-line inline prompt that re-describes skills.

Approach: Plugin Tools vs. Skills vs. Scripts

Three extraction mechanisms, ordered by LLM removal:

Mechanism LLM Involvement Best For
Skills LLM still executes; saves tokens via dedup Shared instructions requiring judgment
Plugin Tools LLM calls tool, gets structured result; no interpretation Deterministic procedures, config lookups, API operations
Shell Scripts LLM calls bash, interprets stdout Superseded by plugin tools (typed I/O, testable, composable)

Decision: Phase 2 scripts and Phase 3 config extraction are replaced by plugin tools in a new uf-workflow plugin. Plugin tools have full access to Node.js fs, BunShell, and the OpenCode client API — everything shell scripts can do, plus Zod-validated I/O, AbortSignal cancellation, and vitest testing.

Standard Result Envelope

All uf-workflow tools use a standard result shape:

type ToolSuccess<T> = {
  status: "ok"
  data?: T              // query tools: the answer; side-effect tools: omitted or summary
}

type ToolFailure = {
  status: "error"
  message: string       // human-readable, always present
  retryable: boolean    // can the agent try again?
  code?: string         // machine-readable error category
}

type ToolResult<T> = ToolSuccess<T> | ToolFailure

Output verbosity rules:

  • Side-effect success: Minimal — { status: "ok" } or { status: "ok", data: "3 labels applied" }
  • Query success: The answer only — { status: "ok", data: { family: "claude-opus-4" } }
  • Failure: Actionable detail — message, retryable flag, error code

Out of Scope

  • uf.init.md conversion (deferred)

Phase 1: Skills + Agent Refactoring (Low Effort, High Impact)

Skills deduplicate shared instructions that require LLM judgment.

Phase 1 total: ~12,000–13,000 tokens saved

Phase 2: uf-workflow Plugin + Tools (Medium Effort, High Impact)

Plugin tools replace prose-described deterministic procedures. The LLM calls a tool and gets a structured result — no stdout parsing, no polling loops, no config interpretation.

Foundation

GitHub API Tools

Workflow Tools

Config Lookup Tools

Agent Executor (cross-plugin)

Phase 2 total: ~3,750+ tokens saved from prose, plus additional savings from LLM no longer reading/interpreting config files

Phase 3: Structural Cleanup (Low Effort)

Phase 3 total: ~350 tokens saved


Cross-Cutting Consolidation

Several procedures repeat across 3-5 command files. A single tool eliminates ALL instances:

Pattern Files Affected Instances Tool
Temp file → GH API POST → cleanup finale, review-pr, review-council, triage-issue, address-feedback 5+ post_github_review in uf-workflow
PR metadata fetch review-pr, review-council, address-feedback 3 fetch_pr_context (future)
Divisor agent discovery review-pr, review-council, triage-issue 3 Already in plan_review_dispatch
Convention pack loading review-pr, review-council 2 list_convention_packs
Model name extraction finale, address-feedback, review-pr 3 resolve_model_name
CLI prereqs check review-pr, triage-issue, address-feedback 3 check_cli_prereqs (future)

Plugin Architecture

.opencode/plugins/
├── invoke-agent/          # Agent invocation (exists)
├── review-dispatch/       # Review planning + dispatch (exists, gains dispatch_agent_run from #680)
└── uf-workflow/           # NEW: deterministic UF workflow operations
    └── index.ts           # ~13 tools sharing BunShell + fs dependencies

Acceptance Criteria

  • All Phase 1 stories completed
  • uf-workflow plugin created with standard result envelope
  • All Phase 2 tools implemented and tested
  • Phase 3 cleanup completed
  • No command file exceeds 800 lines (currently 6 files exceed this)
  • No agent file contains duplicated boilerplate from other agents
  • All deterministic procedures use plugin tools, not prose
  • All tools follow standard result envelope (minimal success, detailed failure)
  • Plugin tests pass (make plugin-test)

Activity

  1. added
    enhancement
    refactor
    on Oct 5, 2026
  2. self-assigned this
    on Oct 5, 2026
  3. added 2 commits that reference this issue on Oct 13, 2026
    31b6582
    f2d9143
  4. jflowers commented on Oct 19, 2026

    @jflowers
    ContributorAuthor

    Tracker maintenance pass on the Phase 1–3 children (2026-10-19), applying the Router Pattern direction from #716 retroactively:

    Phase 1 skills #660, #661, #663 remain correctly scoped as skills — they deduplicate judgment-bearing instructions, which is the right mechanism for those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementrefactor

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions