Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

feat: cache Anthropic system prompts

nash_sueaa597696a11
diff --git a/src/lib/__tests__/llm-providers.test.ts b/src/lib/__tests__/llm-providers.test.ts
index 091ddc33..0106b5c0 100644
--- a/src/lib/__tests__/llm-providers.test.ts
+++ b/src/lib/__tests__/llm-providers.test.ts
@@ -32,7 +32,10 @@ function buildMiniMaxProviderConfig(config: LlmConfig) {
buildBody: (messages: Array<{ role: string; content: string }>) => {
const systemMessages = messages.filter((m) => m.role === "system")
const conversationMessages = messages.filter((m) => m.role !== "system")
- const system = systemMessages.map((m) => m.content).join("\n") || undefined
+ const systemText = systemMessages.map((m) => m.content).join("\n")
+ const system = systemText
+ ? [{ type: "text", text: systemText, cache_control: { type: "ephemeral" } }]
+ : undefined
return {
messages: conversationMessages,
...(system !== undefined ? { system } : {}),
@@ -96,13 +99,19 @@ describe("MiniMax Provider", () => {
expect(body.model).toBe("MiniMax-M2.7")
})
- it("separates system messages from conversation (Anthropic convention)", () => {
+ it("separates system messages from conversation and marks the system prompt cacheable", () => {
const cfg = buildMiniMaxProviderConfig(makeConfig())
const body = cfg.buildBody([
{ role: "system", content: "You are helpful" },
{ role: "user", content: "Hello" },
]) as Record<string, unknown>
- expect(body.system).toBe("You are helpful")
+ expect(body.system).toEqual([
+ {
+ type: "text",
+ text: "You are helpful",
+ cache_control: { type: "ephemeral" },
+ },
+ ])
expect(body.messages).toEqual([{ role: "user", content: "Hello" }])
})
})
diff --git a/src/lib/llm-providers.test.ts b/src/lib/llm-providers.test.ts
index 27d560cc..dffb0626 100644
--- a/src/lib/llm-providers.test.ts
+++ b/src/lib/llm-providers.test.ts
@@ -106,8 +106,32 @@ describe("Anthropic buildBody — vision content", () => {
const body = getProviderConfig(cfg).buildBody([
sys,
{ role: "user", content: "ok" },
- ]) as { system?: string; messages: unknown[] }
- expect(body.system).toBe("be terse")
+ ]) as { system?: unknown; messages: unknown[] }
+ expect(body.system).toEqual([
+ {
+ type: "text",
+ text: "be terse",
+ cache_control: { type: "ephemeral" },
+ },
+ ])
+ })
+
+ it("emits Anthropic system prompts as cacheable text blocks", () => {
+ const cfg = mkConfig({ provider: "anthropic", model: "claude-3-5-sonnet-latest" })
+ const body = getProviderConfig(cfg).buildBody([
+ { role: "system", content: "You are helpful." },
+ { role: "system", content: "Prefer concise answers." },
+ { role: "user", content: "Hi" },
+ ]) as { system?: unknown; messages: unknown[] }
+
+ expect(body.system).toEqual([
+ {
+ type: "text",
+ text: "You are helpful.\nPrefer concise answers.",
+ cache_control: { type: "ephemeral" },
+ },
+ ])
+ expect(body.messages).toEqual([{ role: "user", content: "Hi" }])
})
})
@@ -365,6 +389,29 @@ describe("reasoning controls", () => {
expect(provider.headers["x-api-key"]).toBeUndefined()
})
+ it("uses cacheable system blocks for custom Anthropic-wire providers", () => {
+ const cfg = mkConfig({
+ provider: "custom",
+ apiKey: "sk-custom",
+ model: "custom-claude",
+ customEndpoint: "https://example.com/anthropic",
+ apiMode: "anthropic_messages",
+ })
+ const body = getProviderConfig(cfg).buildBody([
+ { role: "system", content: "Project-wide instruction." },
+ { role: "user", content: "Hi" },
+ ]) as { system?: unknown; messages: unknown[] }
+
+ expect(body.system).toEqual([
+ {
+ type: "text",
+ text: "Project-wide instruction.",
+ cache_control: { type: "ephemeral" },
+ },
+ ])
+ expect(body.messages).toEqual([{ role: "user", content: "Hi" }])
+ })
+
it("disables Qwen3 thinking on OpenAI-compatible local endpoints", () => {
const cfg = mkConfig({
provider: "custom",
diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.ts
index d99d80bb..b7fb5827 100644
--- a/src/lib/llm-providers.ts
+++ b/src/lib/llm-providers.ts
@@ -371,11 +371,10 @@ function buildOpenAiCompatibleBody(
* block, and uses a different shape than OpenAI for images
* (`source.media_type` + `source.data` instead of a `data:` URL).
*
- * For system messages, Anthropic accepts the top-level `system`
- * field as a string OR as a content-block array. We always
- * stringify system content here because every existing system-
- * prompt call site sends a string and the round-trip through
- * blocks is lossy.
+ * System messages are flattened separately into the top-level
+ * `system` field. Anthropic accepts that field as either a string or a
+ * text-block array; we use the block array form there so the system
+ * prompt can opt into prompt caching.
*/
function toAnthropicContent(content: string | ContentBlock[]): unknown {
if (typeof content === "string") return content
@@ -396,7 +395,7 @@ function toAnthropicContent(content: string | ContentBlock[]): unknown {
}
/**
- * Anthropic's top-level `system` field is a string, not blocks.
+ * Anthropic's top-level `system` field accepts a string or text blocks.
* If a caller puts images inside a system message we drop them —
* Anthropic doesn't accept system-level images today, and silently
* losing them is the lesser evil compared to the request 400ing
@@ -409,6 +408,17 @@ function flattenAnthropicSystem(content: string | ContentBlock[]): string {
.join("")
}
+function buildAnthropicSystem(systemText: string): unknown[] | undefined {
+ if (!systemText) return undefined
+ return [
+ {
+ type: "text",
+ text: systemText,
+ cache_control: { type: "ephemeral" },
+ },
+ ]
+}
+
function buildAnthropicBody(
messages: ChatMessage[],
overrides?: RequestOverrides,
@@ -417,9 +427,10 @@ function buildAnthropicBody(
const conversationMessages = messages
.filter((m) => m.role !== "system")
.map((m) => ({ role: m.role, content: toAnthropicContent(m.content) }))
- const system =
- systemMessages.map((m) => flattenAnthropicSystem(m.content)).join("\n") ||
- undefined
+ const systemText = systemMessages
+ .map((m) => flattenAnthropicSystem(m.content))
+ .join("\n")
+ const system = buildAnthropicSystem(systemText)
// Anthropic Messages uses top_p / top_k (Python-style snake_case), a
// mandatory `max_tokens`, and `stop_sequences` instead of `stop`.