Let Codex Audit Its Own Token Spending
Context bloat drives token costs in AI agent workflows, and this self-audit prompt gives Codex users a repeatable way to find and cut that waste. The technique transfers to any agent that can inspect its own execution history, as long as the suggested rules are validated against real tasks.
Codex's token bill rarely comes from code generation. Waste piles up when the agent loads whole files to answer a function-level question, stuffs thousands of log lines into context, or passes its entire conversation history to a sub-agent that needs only one fact. A prompt trick gets Codex to review its own run history and name those leaks, then propose concrete rules: read file parts after search, truncate tool output, retry failures only after checking the error, hand sub-agents minimal context, and load only the skills the current job needs.
Running it produced five optimization rules that target context bloat rather than model capability. The prompt itself is simple: ask Codex to analyze past runs for token savings without sacrificing performance. The caveat is explicit: treat the diagnosis as a starting point, not a directive. Benchmark a few common tasks before and after, and keep a rule only if it cuts tokens without hurting output quality.
The same prompt can scale into a project knowledge distiller. The agent's introspection is based on your past conversations, failure cases, and task records, which can be turned into reusable assets like workflows, structure docs, and optimized prompts. Token savings are the first layer; the second layer is converting scattered session history into structured project experience.
Token costs in agent workflows are dominated by context loading and tool output, not by generation, so optimization efforts should target context management first.
Self-diagnosis works because the agent sits closer to its own execution traces than a human does, but its recommendations are hypotheses, not ground truth, and need empirical verification.
The bigger win behind token savings is converting scattered conversation history into structured, reusable project knowledge.