Skip to main content

tokmd

CI PyPI License

Read this in Español.

tokmd is a command-line tool that counts tokens per section of a Markdown file, using the right tokenizer for your target AI platform (Claude, OpenAI, and more).

Unlike flat counters, tokmd parses Markdown documents into a hierarchical section tree based on ATX headings (# through ######) and YAML front matter. Token counts roll up through the hierarchy just like du rolls up directory sizes: limiting display depth never loses token visibility.


Installation

From PyPI (once published)

Run directly without manual installation using uvx:

uvx tokmd --help

Or install via pip:

pip install tokmd

From source (development)

Clone the repository and run with uv:

git clone https://github.com/fferdgelis/tokmd.git
cd tokmd
uv run tokmd --help

Quick Example (Real Output)

Given a Markdown document (in this example, docs/ADR/ADR-004-empaquetado-y-publicacion.md from this repository), running tokmd with the Claude tokenizer:

$ tokmd docs/ADR/ADR-004-empaquetado-y-publicacion.md --platform claude-code
(front matter): 368
  ADR-004 — Empaquetado y publicación: nombre, licencia y canal: 868
    Historial de modificaciones: 172
    Estado: 44
    Contexto: 130
    Decisiones: 394
    Consecuencias: 64
    Verificación y reversibilidad: 64

Notice that:

  • Each section's token count includes its own text plus all nested subsections beneath it (e.g. ADR-004 accumulates 868 tokens total).
  • Front matter is identified and measured as its own block (368 tokens).

Usage

tokmd [OPTIONS] FILE

Platforms

The --platform option automatically selects the appropriate tokenizer and encoding for your target environment:

Platform Tokenizer engine Default model / encoding
claude-code Anthropic Claude (ctok) Claude 3.5 / 4.x family (4.8)
codex OpenAI (tiktoken) o200k_base (GPT-4o, GPT-5)
opencode Dynamically resolved Requires --model (e.g. --model claude-opus-5 or --model gpt-5)
antigravity Planned for v1.1 Can be overridden using --tokenizer

Command Options

  • --platform [claude-code|codex|opencode|antigravity] (required): Target platform.
  • --model TEXT: Model identifier, required when --platform opencode is selected.
  • --tokenizer [claude|openai]: Override the platform's default tokenizer.
  • --format [table|md|json|csv]: Output format (default: table).
    • table: Indented plain text hierarchy.
    • md: Indented Markdown bullet list.
    • json: JSON array with title, level, and accumulated tokens.
    • csv: CSV format with headers level,title,tokens.
  • --depth INTEGER: Limit output to sections up to this heading level. Deeper sections are rolled up into their parent totals.
  • --sort [document|tokens]: Order rows by original document order (document, default) or sort sibling sections by descending accumulated tokens (tokens).
  • --claude-family TEXT: Override Claude tokenizer family ("3.0", "4.7", "4.8").
  • --encoding TEXT: Override tiktoken encoding (e.g. "o200k_base", "cl100k_base").
  • --version: Show version and exit.
  • --help: Show CLI help and options.

Advanced Examples

Limiting depth (--depth)

$ tokmd docs/ADR/ADR-004-empaquetado-y-publicacion.md --platform claude-code --depth 1
(front matter): 368
  ADR-004 — Empaquetado y publicación: nombre, licencia y canal: 868

Sorting by token count (--sort tokens)

$ tokmd docs/ADR/ADR-004-empaquetado-y-publicacion.md --platform claude-code --sort tokens
  ADR-004 — Empaquetado y publicación: nombre, licencia y canal: 868
    Decisiones: 394
    Historial de modificaciones: 172
    Contexto: 130
    Consecuencias: 64
    Verificación y reversibilidad: 64
    Estado: 44
(front matter): 368

Markdown list output (--format md)

$ tokmd docs/ADR/ADR-004-empaquetado-y-publicacion.md --platform claude-code --format md
- (front matter): 368
  - ADR-004 — Empaquetado y publicación: nombre, licencia y canal: 868
    - Historial de modificaciones: 172
    - Estado: 44
    - Contexto: 130
    - Decisiones: 394
    - Consecuencias: 64
    - Verificación y reversibilidad: 64

Credits & Acknowledgements

tokmd builds upon and is inspired by the work of the open-source community:

  • ctok by Sander Land (MIT License): Used for offline reconstruction and counting with Anthropic Claude's tokenizer without requiring API keys or network access. tokmd is not affiliated with Anthropic or the ctok project.
  • ttok by Simon Willison (Apache License 2.0): The command-line interface design and platform-first philosophy of tokmd were inspired by ttok.
  • tiktoken by OpenAI (MIT License): Fast BPE tokenizer library used for OpenAI model token counts.

For further architectural context, see NOTICE and docs/ADR/ADR-001-eleccion-de-motores-de-tokenizacion.md.


License

This project is licensed under the Apache License, Version 2.0. See LICENSE for details.

Copyright (c) 2026 Fabián Ferdgelis.

Release files for tokmd 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokmd 1.0.0
File Size Uploaded
tokmd-1.0.0.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokmd 1.0.0
File Interpreter ABI Platform
tokmd-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / tokmd-1.0.0.tar.gz

Download URL tokmd-1.0.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6401a7c9c77486d1b18881e0d2b1ace0e8c05b8e7f6051f42243a15e6104003d
BLAKE2b-256 checksum
How to use checksums
4db64f6ee691eda7937cf3d992d2cb5c28a63bf0b067b99d59fd5c72748e3e50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / tokmd-1.0.0-py3-none-any.whl

Download URL tokmd-1.0.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a50f9550c00da659ea8635096550f2f78fda210209ce70114bbf55ab5bb5ece2
BLAKE2b-256 checksum
How to use checksums
298c21c83229f4f38a18f798a94b9eae74952689f60f4199793823d3c295add9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page