跪拜 Guibai
← All articles
Frontend · Backend · AI Programming

Let Codex Audit Its Own Token Spending

By ikoala ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— A single prompt can make Codex review its past runs and propose specific token-saving adjustments.
— The prompt asks Codex to consider skills, scripts, or model switching without sacrificing performance or capabilities.
— Codex typically examines recent sessions, input/output ratio, tool call results, screenshots, sub-agents, and loaded skills.
— The most common waste: whole-file reads, long raw logs in context, and sub-agents carrying full conversation history.
— Five rules: search then batch-read, use low-res screenshots when possible, don't blind-retry failures, give sub-agents minimal context, load only needed skills.
— Validate any suggestion with A/B tests on high-frequency tasks, comparing token consumption, output quality, tool call count, completion time, and manual intervention.
— The same audit can extend to knowledge distillation, turning conversation history, failure cases, and task patterns into reusable project assets.
Conclusions

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.

Concepts & terms
Token audit
A self-review where an AI agent examines its own past run history to identify where tokens are being wasted and suggest optimization rules.
Skill (in Codex)
A reusable instruction set or rule loaded into the context to guide task-specific behavior. Loading unnecessary skills consumes tokens without helping the current task.
Context bloat
When an agent's context window fills with irrelevant or redundant information—like whole-file reads or verbose tool logs—inflating token costs and degrading reasoning.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗