From 1d0fa74d4a116607a455dc07eb3a75ec8578e342 Mon Sep 17 00:00:00 2001 From: ilya-bov <111734093+ilya-bov@users.noreply.github.com> Date: Sat, 28 Feb 2026 15:21:01 +0300 Subject: [PATCH] perf(agent): optimize context for prompt caching and limit history Round datetime in system prompt to the hour so the prefix stays stable across requests, enabling provider-side prompt caching (Anthropic ~5 min, OpenAI ~1 hr). Trim chat history to 80 messages via the existing History class to prevent context window overflow on long conversations. Co-Authored-By: Claude Opus 4.6 --- src/lib/agent/agent.ts | 11 +++++++++-- src/lib/agent/prompts.ts | 7 +++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib/agent/agent.ts b/src/lib/agent/agent.ts index d0a59fd..25ea99f 100644 --- a/src/lib/agent/agent.ts +++ b/src/lib/agent/agent.ts @@ -13,6 +13,7 @@ import { getChat, saveChat } from "@/lib/storage/chat-store"; import { createAgentTools } from "@/lib/tools/tool"; import { getProjectMcpTools } from "@/lib/mcp/client"; import type { AgentContext } from "@/lib/agent/types"; +import { History } from "@/lib/agent/history"; import type { ChatMessage } from "@/lib/types"; import { publishUiSyncEvent } from "@/lib/realtime/event-bus"; @@ -584,7 +585,10 @@ export async function runAgent(options: { const chat = await getChat(options.chatId); if (chat) { // Convert stored messages to ModelMessage format (including tool calls/results) - context.history = convertChatMessagesToModelMessages(chat.messages); + const allMessages = convertChatMessagesToModelMessages(chat.messages); + const history = new History(80); + history.addMany(allMessages); + context.history = history.getAll(); } // Build tools: base + optional MCP tools from project .meta/mcp @@ -717,7 +721,10 @@ export async function runAgentText(options: { const chat = await getChat(options.chatId); if (chat) { - context.history = convertChatMessagesToModelMessages(chat.messages); + const allMessages = convertChatMessagesToModelMessages(chat.messages); + const history = new History(80); + history.addMany(allMessages); + context.history = history.getAll(); } const baseTools = createAgentTools(context, settings); diff --git a/src/lib/agent/prompts.ts b/src/lib/agent/prompts.ts index 228de6c..66ed14e 100644 --- a/src/lib/agent/prompts.ts +++ b/src/lib/agent/prompts.ts @@ -201,9 +201,12 @@ export async function buildSystemPrompt(options: { } } - // 6. Current date/time + // 6. Current date/time (rounded to the hour for prompt caching) + const now = new Date(); + now.setMinutes(0, 0, 0); + const dateStr = now.toISOString().slice(0, 13) + ":00:00Z"; parts.push( - `\n## Current Information\n- Date/Time: ${new Date().toISOString()}\n- Timezone: ${Intl.DateTimeFormat().resolvedOptions().timeZone}` + `\n## Current Information\n- Date/Time: ${dateStr}\n- Timezone: ${Intl.DateTimeFormat().resolvedOptions().timeZone}` ); return parts.join("\n\n");