v0.10.5

Session Cache Keys

Last updated


How a session ID attached to the context becomes a per-provider prompt-cache key or session-affinity header.

Attaching a session


  1. ctx = llmrouter.WithSessionID(ctx, conversationID)
  2. out, code, err := agent.Send(ctx, messages, nil, llmrouter.ReasoningDefault, llmrouter.ModeDefault)

WithSessionID trims the ID and returns the context unchanged when it is empty, so passing an unset ID is safe. SessionID(ctx) reads it back and returns "" for a nil context.

Derived key


Adapters never send the raw ID. SessionUUID(ctx) hashes it with SHA-256 and formats the first 128 bits as a UUID-shaped string (version nibble 8, variant nibble a). The same conversation therefore maps to the same key on every request, while the caller's own identifier never leaves the process.

Per-provider mapping


ProviderWhere the key goesApplies to
openaibody prompt_cache_keyChat Completions and Responses, Send and SendStream
codexheader session_id and body prompt_cache_keySend and SendStream
grok, grok-oauthbody prompt_cache_key (built by core/xai)Send and SendStream
cloudflareheader x-session-affinitySend (streaming is unsupported)
claude-codenot sent; selects the long-lived CLI process for the conversationSend (see Claude Code CLI)
every other providerignored—

Cache TTL


  1. ctx = llmrouter.WithCacheTTL(ctx, "1h")

WithCacheTTL attaches a prompt-cache TTL the same way: it trims the value and returns the context unchanged when it is empty, and CacheTTL(ctx) reads it back. Only claude-code reads it: 1h sets the CLI's one-hour prompt cache and, with a StateDir, persists the session; any other value uses the five-minute default.

中文