Token-Aware Context Packing for Codex CLI (Relevance-per-Token or Knapsack Heuristic)

Resolved 💬 2 comments Opened Oct 13, 2025 by Aki-07 Closed Dec 14, 2025

What feature would you like to see?

The Codex CLI currently builds the prompt by concatenating conversation history and project context sequentially, trimming only by byte or message count.
This simple approach can cause large chunks to dominate the token window, pushing out smaller, high-value snippets.

I’d like to propose a token-aware context selection strategy that ranks candidate chunks by relevance-per-token ratio (or a small hybrid knapsack heuristic).
This would prioritize concise but highly relevant code segments and improve coverage under a fixed token budget.

Key ideas:

  • Compute score / token_cost for each candidate chunk.
  • Optionally “pin” the top few high-relevance items, then fill the remaining window using ratio-ordered selection.
  • For modest candidate sets (≤200 chunks), a 0/1 knapsack dynamic-programming solver could provide optimal packing.
  • Add guardrails to ensure at least one snippet per file or per user-selected region.

Expected benefits:

  • Better recall for small utility helpers.
  • Fewer prompt overflows.
  • More stable latency and lower overall token usage.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗