Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

perf(core): keep the token budget out of the cached system prompt

Salman Qureshie2dddd4e6f56
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 88d79002..76cbac7a 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -86,7 +86,7 @@ import { classifyStreamCompletion, isLengthTruncation, truncationContinuationPro
import { MAX_EXECUTE_CONTINUATIONS, MAX_VERIFICATION_CONTINUATIONS, executeContinuationPrompt, shouldForceExecuteContinuation, isFailedToolResult, shouldRequireVerification, isVerificationOutputOk, verificationPrompt, responseAsksUser, EXECUTE_MUTATING_TOOLS, VERIFICATION_COMMAND_PATTERN, wakeUpPrompt } from './execute-guard.js';
import { classifyTurnEnd, stepsExhaustedPrompt, STEPS_PAUSED_BANNER, WORK_NOT_STARTED_BANNER, VERIFICATION_FAILED_BANNER, type LoopEndCause } from './completion-verdict.js';
import { HISTORY_TOKEN_BUDGET, HISTORY_TOKEN_BUDGET_SAVER, TOOL_TRACE_KIND, selectHistoryWindow, toModelMessage, withReasoningParts, formatToolTrace, formatToolTraceLine, summarizeToolArgs, summarizeToolResult, detectRepetitionLoop } from './context-window.js';
-import { buildSystemPrompt as assembleSystemPrompt } from './system-prompt.js';
+import { buildSystemPrompt as assembleSystemPrompt, budgetContext } from './system-prompt.js';
import { buildStatusVerbPrompt, parseStatusVerbs, shouldRefreshStatusVerbs } from './status-verbs.js';
import { verbPoolFor } from '../ui/status-word.js';
import { StallWatchdog } from './stall-watchdog.js';
@@ -2439,6 +2439,7 @@ export class Agent {
// fake user/assistant dialogue: the fake "Noted. I'll keep this in
// mind." turns primed terse acknowledgements and polluted history.
const contextBlocks: string[] = [];
+ contextBlocks.push(budgetContext(this.tokenBudget.getStatusText(), this.tokenBudget.getUsagePercentage()));
const loopWarning = detectRepetitionLoop(recentMemory);
if (loopWarning) contextBlocks.push(loopWarning);
@@ -4032,8 +4033,6 @@ export class Agent {
botSection: this.botManager ? this.botManager.getSystemPromptSection() : '',
programmingSuffix: this.programmingMode.getSystemPromptSuffix(),
researchSuffix: this.researchMode.getSystemPromptSuffix(),
- budgetStatus: this.tokenBudget.getStatusText(),
- budgetUsagePercentage: this.tokenBudget.getUsagePercentage(),
saverSuffix: this.saverMode.getSystemPromptSuffix(),
cwd: this.capabilities.getCwd(),
now: new Date(),
diff --git a/src/core/system-prompt.test.ts b/src/core/system-prompt.test.ts
index f0db3cd8..d0169db8 100644
--- a/src/core/system-prompt.test.ts
+++ b/src/core/system-prompt.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { buildSystemPrompt, environmentSection, type SystemPromptInputs } from './system-prompt.js';
+import { budgetContext, buildSystemPrompt, environmentSection, type SystemPromptInputs } from './system-prompt.js';
const base: SystemPromptInputs = {
identityPrompt: 'You are Mercury.',
@@ -7,8 +7,6 @@ const base: SystemPromptInputs = {
botSection: '',
programmingSuffix: '',
researchSuffix: '',
- budgetStatus: 'Token budget: 1,000 / 100,000 used today.',
- budgetUsagePercentage: 1,
saverSuffix: '',
cwd: '/repo',
now: new Date('2026-10-09T18:42:00Z'),
@@ -23,7 +21,7 @@ describe('system prompt assembly', () => {
it('starts with the identity and keeps the stable section order', () => {
const p = buildSystemPrompt(base);
expect(p.startsWith('You are Mercury.')).toBe(true);
- const order = ['Token budget', 'Environment:', 'Tool Usage Guidelines', 'Second Brain is DISABLED'].map((s) => p.indexOf(s));
+ const order = ['Environment:', 'Tool Usage Guidelines', 'Second Brain is DISABLED'].map((s) => p.indexOf(s));
expect(order.every((v, i) => v > -1 && (i === 0 || v > order[i - 1]))).toBe(true);
});
@@ -35,11 +33,16 @@ describe('system prompt assembly', () => {
expect(a).toContain('Working directory: /w');
});
- it('adds the concision hint above 70% budget and the saver suffix when given', () => {
- const p = buildSystemPrompt({ ...base, budgetUsagePercentage: 80, saverSuffix: '\n\nSAVER' });
- expect(p).toContain('Be concise to conserve tokens.');
- expect(p).toContain('SAVER');
- expect(buildSystemPrompt(base)).not.toContain('Be concise');
+ it('keeps the per-turn token budget out of the cached prompt', () => {
+ // The budget changes every turn; in the stable prompt it broke the
+ // provider prefix cache and the whole prompt was re-read per request.
+ expect(buildSystemPrompt(base)).not.toContain('Token budget');
+ expect(budgetContext('Token budget: 1,000 / 100,000 used', 1)).toBe('Token budget: 1,000 / 100,000 used');
+ expect(budgetContext('Token budget: 80,000 / 100,000 used', 80)).toContain('Be concise to conserve tokens.');
+ });
+
+ it('adds the saver suffix when given', () => {
+ expect(buildSystemPrompt({ ...base, saverSuffix: '\n\nSAVER' })).toContain('SAVER');
});
it('describes Second Brain when enabled, including the paused state', () => {
diff --git a/src/core/system-prompt.ts b/src/core/system-prompt.ts
index 0ead273c..54f9bcc5 100644
--- a/src/core/system-prompt.ts
+++ b/src/core/system-prompt.ts
@@ -4,7 +4,8 @@
* budget, memory summary, tool names, GitHub config) and this module turns
* them into the prompt text. Everything here is the STABLE prefix of a turn;
* per-turn context (retrieved memory, loop warnings) goes into the volatile
- * system block built in Agent.handleMessage.
+ * system block built in Agent.handleMessage — the token budget included,
+ * since it changes on every turn.
*/
export interface SystemPromptInputs {
@@ -16,8 +17,6 @@ export interface SystemPromptInputs {
botSection: string;
programmingSuffix: string;
researchSuffix: string;
- budgetStatus: string;
- budgetUsagePercentage: number;
saverSuffix: string;
cwd: string;
now: Date;
@@ -38,6 +37,16 @@ export const TOOL_USAGE_GUIDELINES = `**Tool Usage Guidelines:**
- Do NOT create one-time-use helper scripts. If the user asks you to create a file, create it directly with create_file or write_file.
- When creating multiple files, call create_file or write_file for each one individually. Do not batch them into a script.`;
+/**
+ * Token budget line for the per-turn (volatile) system block. It changes on
+ * every turn, so it must never sit in the stable prompt: there it broke the
+ * provider's prefix cache for the tool schemas and everything after it,
+ * and the whole ~12K-token prompt was re-read on every request.
+ */
+export function budgetContext(statusText: string, usagePercentage: number): string {
+ return usagePercentage > 70 ? `${statusText}\nBe concise to conserve tokens.` : statusText;
+}
+
/** Date at day resolution and time at hour resolution: the prompt is cached as a prefix. */
export function environmentSection(now: Date, timezone: string, platform: string, cwd: string): string {
const dateStr = now.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZone: timezone });
@@ -109,8 +118,6 @@ export function buildSystemPrompt(i: SystemPromptInputs): string {
if (i.botSection) prompt += i.botSection;
if (i.programmingSuffix) prompt += i.programmingSuffix;
if (i.researchSuffix) prompt += i.researchSuffix;
- prompt += '\n\n' + i.budgetStatus;
- if (i.budgetUsagePercentage > 70) prompt += '\nBe concise to conserve tokens.';
if (i.saverSuffix) prompt += i.saverSuffix;
prompt += '\n\n' + environmentSection(i.now, i.timezone, i.platform, i.cwd);
prompt += '\n\n' + TOOL_USAGE_GUIDELINES;