codex-token-guard AGENTS.md

Product and architecture instructions for codex-token-guard, a Codex plugin intended to reduce unnecessary session text while measuring its effects.

In plain words
What is it for?
Use it when building or changing hooks, token estimates, command policies, output compression, session memory, metrics, benchmarks, or quality checks.
Why use it?
It defines the plugin’s goals, limits, lifecycle hooks, and measurements so token reduction does not remove information needed to complete tasks.

Instructions file for CodexOpenCode

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add instructions/davidcreador/codex-token-guard/agents-md
Clone the repo
git clone --depth 1 https://github.com/Davidcreador/codex-token-guard

Made for: Codex, OpenCode.

Per session 1,001 This file is loaded in full into every session.
When invoked 1,001 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.01001 $0.01001
Opus 5 $0.00500 $0.00500
Sonnet 5 $0.00200 $0.00200
Haiku 4.5 $0.00100 $0.00100

Measured 2d ago against content hash 690cbac4f4bf, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

codex-token-guard AGENTS.md scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

AGENTS.md · 240 lines

How it starts

The opening of the file, as written. The whole thing — 240 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Build codex-token-guard, a plug-and-play Codex plugin that reduces token usage during Codex sessions without reducing output quality.

The plugin should use Codex lifecycle hooks where possible, especially:

  • PreToolUse
  • PostToolUse
  • PreCompact
  • PostCompact
  • UserPromptSubmit
  • Stop

The plugin must be measurable. It should track token savings, blocked commands, compressed outputs, session summaries, and quality impact.

Product Requirements

Main goal

Reduce unnecessary context and tool-output tokens in Codex sessions.

Non-goals

  • Do not rewrite Codex internals.
  • Do not depend on unsupported Codex APIs.
  • Do not aggressively remove information required for task success.
  • Do not make Codex worse at coding.

Architecture

Create this structure:

codex-token-guard/
  .codex-plugin/
    plugin.json
  hooks/
    hooks.json
    pre-tool-use.ts
    post-tool-use.ts
    pre-compact.ts
    post-compact.ts
    user-prompt-submit.ts
    stop.ts
  src/
    cli.ts
    config.ts
    token-estimator.ts
    command-policy.ts
    output-compressor.ts
    session-memory.ts
    metrics.ts
    benchmark.ts
    quality.ts
  skills/
    token-guard/
      SKILL.md
  benchmarks/
    fixtures/
    tasks/
  .codex-token-guard/
    metrics.jsonl
    session.md
    decisions.md
    files-touched.json
    errors.md

Plugin behavior
1. PreToolUse

Before Bash commands run, inspect the command and classify it:
safe
expensive
dangerous
needs_confirmation

Block or warn on:
cat large files
tree
find .
grep -r
npm test
pnpm test
yarn test
bun test
large log dumps
recursive ls
printing lockfiles
printing generated files
printing node_modules
printing dist/build/.next

Suggest cheaper alternatives:
cat file.ts -> sed -n '1,160p' file.ts
find . -> rg --files | head -200
grep -r "x" . -> rg "x"
npm test -> pnpm test path/to/specific.test.ts
tree -> rg --files | sed -n '1,200p'

2. PostToolUse
Compress noisy outputs.

keep:
errors
stack traces
file paths
line numbers
failed assertions
test names
compiler errors
diff summaries
important warnings

Remove:
repeated lines
progress bars
install noise
ANSI colors
huge successful logs
dependency tree spam
duplicated stack frames

3. PreCompact
Generate a clean session summary:
# Session State

## Goal

## Current Status

## Key Decisions

## Files Touched

## Bugs / Errors

## Important Context To Preserve

## Next Steps

4. Stop
Persist durable session memory:
.codex-token-guard/session.md
.codex-token-guard/decisions.md
.codex-token-guard/files-touched.json
.codex-token-guard/errors.md
.codex-token-guard/metrics.jsonl

Metrics
Track:
estimated_raw_tokens
estimated_sent_tokens
estimated_saved_tokens
savings_percentage
commands_blocked
commands_rewritten
outputs_compressed
raw_output_tokens
compressed_output_tokens
compression_ratio
session_duration
quality_flags

Expose CLI commands:
codex-token-guard init
codex-token-guard install
codex-token-guard mode safe
codex-token-guard mode balanced
codex-token-guard mode aggressive
codex-token-guard stats
codex-token-guard snapshot
codex-token-guard restore
codex-token-guard benchmark

Modes

safe

Only block obvious waste.

balanced

Default mode. Compress noisy output and suggest targeted commands.

aggressive

Strict budgets, strong output compression, more blocking.

Quality protection

Never remove:
actual error messages
file paths
line numbers
API names
function names
test names
user instructions
recent edits
architecture decisions

If uncertain, preserve the content.

Benchmarking

Add a benchmark runner that compares:
vanilla Codex
vs
Codex + Token Guard

Metrics:
token savings %
task success
latency
number of retries
number of failed attempts

Generate:
benchmarks/results/report.md

Implementation Requirements

Use TypeScript.

Use fast local token estimation. If exact tokenizer is unavailable, use a configurable approximation:
1 token ≈ 4 characters

Store metrics as JSONL so they are easy to inspect.

Keep hook scripts fast. Any hook should fail open unless in aggressive mode.

Important product rule: **default to quality over savings**.

The MVP should prove this:

```txt
20–40% fewer wasted tokens
near-zero quality loss
easy install
visible savings dashboard

Read the full file on GitHub · 240 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 240 lines · 1,001 tokens per session scan A 690cbac4f4bf

Subscribe to this mod's changes

codex-token-guard AGENTS.md is an instructions file published in the GitHub repository Davidcreador/codex-token-guard (2 stars, last pushed 2mo ago), licensed MIT. It adds 1,001 tokens to every session, about $0.0050 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.