Skip to content

Agent Markdown & Prompt Token Optimisation Framework

Diagnostic guide and TypeScript AST parser to strip redundant markdown tokens, collapse structural whitespace, and reduce agent prompt costs.

By Gordon Geraghty·MIT Licence·Updated: 21 August 2026·INTERMEDIATE·GitHub Mirror ↗

Reducing Token Tax in Long-Running Agents

Autonomous agents resend instructions on every step. Stripping decorative formatting and redundant token structures cuts cumulative token consumption by up to 35% without degrading reasoning fidelity.

Implementation Code & Script

Markdown Token Density Compressorcompress-agent-markdown.tstypescript

Strips decorative markdown characters, removes repeated whitespace, and flattens list syntax.

export function compressAgentMarkdown(input: string): string {
  return input
    .replace(/^\s*[\-\*\+]\s+/gm, '- ') // Standardise bullet points
    .replace(/\n{3,}/g, '\n\n')           // Collapse multiple empty lines
    .replace(/\*\*([^\*]+)\*\*:/g, '$1:')  // Strip bold tags from inline headers
    .replace(/^[\s\t]+/gm, '')             // Strip leading whitespace
    .trim();
}

How to cite and attribute this tool

MIT Licence

This resource is free, open and un-gated under the MIT Open Source Licence. You are encouraged to use, integrate and cite it with attribution:

Geraghty, G. (2026). Agent Markdown & Prompt Token Optimisation Framework. Gordon Geraghty Resources Hub. https://gordongeraghty.com/resources/ai-engineering/agent-markdown-optimization-framework
BibTeX Format
@misc{geraghty_agent_markdown_optimization_framework,
  author = {Geraghty, Gordon},
  title = {Agent Markdown & Prompt Token Optimisation Framework},
  year = {2026},
  url = {https://gordongeraghty.com/resources/ai-engineering/agent-markdown-optimization-framework},
  note = {Head of Performance, Empire Amplify}
}

Changelog & Version History

  • v1.0.0Initial release of agent markdown token compressor.