Skip to content

Studio llama.cpp: the token-count memo renders again on a new day - #17090

Merged
danielhanchen merged 2 commits into
mainfrom
studio/token-count-memo-date
Nov 9, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio/token-count-memo-date

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

#16756 memoises Studio's llama.cpp chat token count, so a tool round that recounts the prompt it just fitted reuses one /apply-template + /tokenize render. The memo key covers the llama-server process, the model and every render input Studio sends, with an 8-entry LRU and no expiry.

It does not cover the clock. llama.cpp builds date_string from the current time and gives Jinja templates strftime_now, and published templates print today's date (the Llama 3.2 template's "Today Date"). The same request rendered after midnight is a different prompt, possibly with a different token count, but the memo returns the old count for as long as the entry stays in the LRU. Reproduced with that template and a mocked server whose date moved from March 31 to April 1: the memo returned the March count (111 tokens) where a fresh render gives 112.

Change

The memo key also holds the local date (time.strftime("%Y-%m-%d"); llama-server is a local process on the same clock). Within a day, nothing changes: identical recounts still hit. On a new day the first count renders again.

Cost: one time.strftime call per count, next to an HTTP round trip.

Tests

test_a_new_day_renders_again: two identical counts on one day render once, the same count the next day renders again.

arm test_llama_count_memo.py
main (a174823) 12 passed, 1 failed (the new test: no render on the new day)
this PR 13 passed

Not covered

  • A template that prints the time of day (strftime_now("%H:%M")) can still be off by its time string within one day. That spelling is not in the templates llama.cpp ships, and the count only sizes the context window.

The memo key held everything the request sends to /apply-template but not the date, and llama.cpp
renders date_string / strftime_now from its own clock (Llama 3.x templates print today's date), so a
count memoised before midnight was returned after it. The local date is now part of the key.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T04:05:10.623560Z c77a5c0 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Nov 8, 2026
[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: c77a5c0453

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant