Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

count_tokens.py crashes on text containing special tokens like <|endoftext|> (tiktoken disallowed_special default) #153

Open

Description

@CloneOfAlex

Describe the bug

count_tokens() in skills/bmad-*-builder/scripts/count_tokens.py guards only import tiktoken and tiktoken.get_encoding(...), then calls enc.encode(text) with tiktoken's default disallowed_special="all". tiktoken raises ValueError ("Encountered text corresponding to disallowed special token '<|endoftext|>'...") when the text contains a special-token string.

Any skill or reference that mentions such a token, for example a skill about tokenizers, prompt formats or LLM evaluation, makes:

  • count_tokens.py <file> exit with a traceback;
  • prepass-prompt-metrics.py crash, since it imports count_tokens and calls it on every file, which aborts the Analyze pre-pass.

(The agent-builder prepass.py swallows the exception and silently falls back to chars // 4 for that file.)

This report is from reading the code: the tiktoken encoding download is blocked in my test sandbox, so I couldn't run the tiktoken path. The behaviour is tiktoken's documented default.

Steps to reproduce

printf -- '---\nname: sk\ndescription: Explains tokenizers. Use when asked.\n---\n# sk\n\nModels end documents with the <|endoftext|> marker.\n' > sk/SKILL.md
uv run skills/bmad-workflow-builder/scripts/count_tokens.py sk/SKILL.md
# expected with tiktoken available: ValueError: Encountered text corresponding to disallowed special token '<|endoftext|>'

Expected behavior

Count special-token text as ordinary text: enc.encode(text, disallowed_special=()). Also cache the encoding at module level. Today get_encoding runs per file, and when the encoding can't be downloaded the pre-passes retry the network for every file before falling back, about 0.2 s/file here.

Environment

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions