Platform Adapters
Relevant source files
The following files were used as context for generating this wiki page:
Platform Adapters constitute the environmental detection and integration layer of the Token Optimizer system. Their primary purpose is to identify the host AI runtime—such as Claude Code, GitHub Copilot, OpenCode, or Codex—and adjust the tool's behavior, file paths, and workflows to match the specific constraints and capabilities of that platform.
By abstracting platform-specific logic, these adapters ensure that auditing and optimization scripts (like measure.py) target the correct configuration directories (e.g., ~/.claude vs ~/.codex) and prevent cross-platform interference, such as running Claude-specific audits in an OpenCode environment. Sources: skills/token-optimizer/SKILL.md:14-41, skills/token-coach/SKILL.md:16-30
Runtime Detection Logic
The system utilizes a multi-step detection process to identify the active platform. This process begins with an environment-only "pre-gate" check that avoids touching disk paths prematurely, followed by an authoritative check performed by the core measurement script.
Environment Pre-Gate Check
Before resolving any internal script paths, the system inspects specific environment variables to identify the runtime. This prevents non-Claude runtimes from incorrectly resolving or modifying Claude-specific configuration paths.
| Runtime | Primary Environment Signals |
|---|---|
| Claude Code | CLAUDE_PLUGIN_ROOT, CLAUDE_PLUGIN_DATA |
| OpenCode | OPENCODE_BIN, OPENCODE_CONFIG_DIR, OPENCODE_DATA_DIR, OPENCODE_CONFIG, OPENCODE_CLIENT |
| GitHub Copilot | COPILOT_HOME, TOKEN_OPTIMIZER_COPILOT_HOME |
| Codex | TOKEN_OPTIMIZER_RUNTIME=codex |
Sources: skills/token-optimizer/SKILL.md:19-41, skills/token-coach/SKILL.md:16-30
Multi-Step Detection Flow
The following diagram illustrates the logical flow used to determine the active platform adapter and initialize the appropriate workflow.
The detection logic prioritizes explicit environment overrides before falling back to process tree scanning via measure.py report. Sources: skills/token-optimizer/SKILL.md:42-108, skills/token-coach/SKILL.md:31-64
Platform-Specific Configurations
Adapters determine which file paths and measurement strategies are used during an audit. This includes locating the core measure.py script across various installation channels.
Installation Path Resolution
The system searches across multiple platform-specific directories to find the newest version of the measurement utility:
~/.claude/skillsand~/.claude/plugins/cache~/.codex/skillsand~/.codex/plugins/cache~/.config/opencode/plugins
Sources: skills/token-optimizer/SKILL.md:88-100, skills/token-coach/SKILL.md:46-59
Feature Availability by Platform
Adapters enable or disable specific optimization features based on the detected runtime's capabilities.
| Feature | Claude Code Support | Codex Support |
|---|---|---|
| Core Config | CLAUDE.md | AGENTS.md |
| Memory File | MEMORY.md | Codex Memories |
| Hooks | Lifecycle Hooks (PreCompact, etc) | Balanced Codex Hooks |
| Optimization | Smart Compaction | Compact Prompt Guidance |
| Model Routing | Opus/Sonnet/Haiku | GPT-5.x Intelligence Levels |
Sources: skills/token-coach/SKILL.md:143-152, skills/token-optimizer/references/optimization-checklist.md:368-375
Cross-Platform Session Continuity
The "Cold-Resume-Lean" feature is implemented across different platform adapters to allow reopening sessions cheaply. It adapts the "lean block" of context based on the checkpoint richness available on the specific platform (Claude Code, Codex, OpenClaw, or OpenCode).
The adapter ensures:
- Token-free reconstruction: Reads local checkpoints and session logs without LLM calls.
- Path-safety: Uses path-prefixes versus CWD to ensure session data does not leak across different project boundaries.
- Idempotency: Savings are credited as
resume_leanevents regardless of the platform.
Sources: skills/token-optimizer/SKILL.md:215-231
Conclusion
Platform Adapters serve as the critical infrastructure for the Token Optimizer, allowing it to function as a universal tool across diverse AI development environments. By strictly isolating runtime-specific logic, the system protects user configurations while providing tailored optimization strategies for each supported AI platform.