Skip to content

fix(ai): the context-budget tokenizer downloads its encoding on first use and silently falls back to a heuristic #1421

Closed

Description

@dan-petty

Problem

_get_tiktoken_encoding (src/devops_cli/ai/context_budget.py:21-32 on release/v0.2.29 at 05e9388) calls tiktoken.encoding_for_model. tiktoken loads the BPE file from its local cache and downloads it from its public blob store when the cache doesn't have it. The function catches every exception, logs it at DEBUG, and returns None. Because it is wrapped in functools.lru_cache, that None is kept, so the process falls back to the 4-characters-per-token heuristic in count_tokens for its whole life, and nothing above DEBUG says so.

Profiling #1363's tests showed collect_context → truncate_to_token_limit reach this call and hit the test suite's socket guard (tests/conftest.py, guarded_connect). So the suite depends on whether the cache is warm, and the context budget in the Service image depends on whether its first call can reach the internet.

Key question: does the Service image have a populated tiktoken cache, and does its network policy allow that download? Check before choosing the remedy.

Proposed remedy

Populate the tiktoken cache at image build (tiktoken reads TIKTOKEN_CACHE_DIR) and in the dev environment's setup, so the encoding loads with no request. When it still can't load, log a WARNING once naming the cache directory, rather than silently switching to the heuristic.

Acceptance criteria

  • With sockets blocked, a test loads the encoding from a prepared cache directory and opens no connection.
  • The Service image build populates the cache, and a test on the Dockerfile or image build asserts it.
  • A failed load logs one WARNING naming the cache directory.
  • Tests are offline, each call phase stays under 1 s, and uv run devops ci passes.

Found while delivering #1363. Related: #1363.

Activity

  1. added
    type/bug
    scope/ai
    source/agent
    on Oct 8, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. Token-counting accuracy and test-suite determinism are degraded, but the system still functions with a coarser estimate rather than failing outright.
    • Value: Medium. Correct token budgeting directly affects how much context reaches the LLM and whether the Service image behaves consistently in air-gapped or restricted-network environments.
    • Effort: Low. The fix is bounded: pre-populate the tiktoken cache at image build, bump the log level to WARNING, and add a few offline tests.
  3. added this to the v0.2.38 milestone on Oct 20, 2026
  4. dan-petty commented on Oct 21, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1770, merged into release/v0.2.38 as ad391be.

    Closes #1421

    7 file(s) changed.

    How it was verified

    Check runs at 67596a4:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • Rule Pack Tests: pass
    • Fail-to-Pass Self-Check: pass
    • Changed-Test Independence: pass
    • Tests & Coverage: pass
    • Fail-to-Pass Check: pass
    • Static Analysis: pass
    • Analyze (python): pass
    • Analyze (actions): pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1421-tiktoken-cache-and-warning.md:

    No Acceptance Criteria section.

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

    scope/aisource/agenttype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions