The number of tables put in the system prompt is decided by a budget that starts from the length of the current question (line 27, test at line 48).
A longer question leaves room for fewer tables, so for a large schema the system prompt is different on different turns: tables the model saw earlier disappear, and the cached prefix (system prompt, then history) does not match.
- What to change
- Spend the table budget without the current question (use a fixed share of max_token), so the system prompt is the same for the whole conversation.
- Why it matters
- The system prompt is the first thing in every request; when it changes, the history after it cannot be read from the cache, and the model sees a different set of tables from turn to turn.
- How often it happens
- The code shows the problem; how often it happens is assumed. To measure it: how many selected schemas exceed half of the model's max_token budget (only those lose tables, and so vary)
- Estimate
- about 180k prompt tokens per 1,000 calls not served from cache
What the estimate assumes
1 in 10 calls hits a schema larger than the budget and changes the table list; about 1,800 tokens of prefix lost each time