Path: redb-route/redb.Route.Llm/Providers/LlmRequest.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Providers/LlmRequest.cs ---- using redb.Route.Llm.Abstractions.Tools; namespace redb.Route.Llm.Providers; /// Single message in the conversation transcript sent to the provider. public sealed class LlmMessage { /// "user", "assistant", "system", or "tool". public required string Role { get; init; } /// Ordered content blocks for this message. public IReadOnlyList Content { get; init; } = []; /// /// Ask the provider to place a prompt-cache breakpoint after this message: everything /// rendered up to and including it (tools, system, the messages before it, this one) becomes a /// cacheable prefix that later requests re-read instead of re-paying. /// /// Meant for a byte-stable preamble that opens every conversation — a fixed opening /// exchange, a few-shot block — placed via . The /// system-prompt breakpoint () stops at the system /// block; without a second one, a preamble sitting in messages is billed in full on /// every turn. Same rules as the system-prompt cache: the prefix must not change a byte, there /// is a model-dependent length floor, and providers without a cache concept ignore the flag. /// Anthropic allows four breakpoints per request; the system one, when asked for, is the /// first. /// public bool CacheBreakpoint { get; init; } /// Convenience constructor for plain-text user messages. public static LlmMessage User(string text) => new() { Role = "user", Content = [new LlmTextBlock(text)] }; /// Convenience constructor for plain-text assistant messages. public static LlmMessage Assistant(string text) => new() { Role = "assistant", Content = [new LlmTextBlock(text)] }; } /// Base type for content blocks (text, tool-use, tool-result, thinking). public abstract record LlmContentBlock; /// Plain text block. public sealed record LlmTextBlock(string Text) : LlmContentBlock; /// /// Tool-use block emitted by the assistant — engine must dispatch the tool and /// reply with a matching . /// public sealed record LlmToolUseBlock(string ToolUseId, string Name, string InputJson) : LlmContentBlock; /// Tool-result block sent back to the assistant on the next turn. public sealed record LlmToolResultBlock(string ToolUseId, string OutputJson, bool IsError = false) : LlmContentBlock; /// /// Token usage reported by the provider. /// /// is the UNCACHED remainder, not the whole prompt. /// The prompt is the sum of all three: what was billed at full price, what was written to cache /// this turn, and what was read back from it. A long agentic run that reports four thousand input /// tokens has not shrunk — the rest came from cache. /// /// Prompt tokens billed at full price. /// Generated tokens. /// /// Tokens written to the cache this turn, billed at a premium over normal input. Staying at zero /// while caching is requested usually means the prefix is below the provider's length floor. /// /// /// Tokens served from cache, billed at a fraction of normal input. **This is the number that says /// caching works.** Zero across repeated calls with an unchanged prefix means something in that /// prefix is not byte-stable. /// public sealed record LlmUsage( int InputTokens, int OutputTokens, int CacheCreationInputTokens = 0, int CacheReadInputTokens = 0) { /// Empty usage record. public static LlmUsage Empty { get; } = new(0, 0); } /// Reason the provider stopped generating. public enum LlmStopReason { /// End of assistant turn. EndTurn, /// Assistant requested one or more tool calls. ToolUse, /// Hit the max-tokens limit. MaxTokens, /// Hit a stop sequence configured on the request. StopSequence, /// Other / unknown reason — see provider-specific raw value. Other } /// Single completion request sent to a provider. public sealed class LlmRequest { /// Model identifier (overrides the factory default when set). public string? ModelId { get; init; } /// Optional system prompt. public string? SystemPrompt { get; init; } /// Conversation history including the current user turn. public IReadOnlyList Messages { get; init; } = []; /// Tool capabilities exposed to the model. Empty = no tools. public IReadOnlyList Tools { get; init; } = []; /// Sampling temperature. public double? Temperature { get; init; } /// Maximum output tokens. public int? MaxTokens { get; init; } /// Top-p sampling parameter. public double? TopP { get; init; } /// Optional stop sequences. public IReadOnlyList? StopSequences { get; init; } /// /// Ask the provider to mark the system prompt as cacheable, so repeated turns re-read it /// instead of paying for it again. /// /// Only worth setting when the system prompt is byte-stable across requests. /// Caching is a prefix match: the provider renders tools, then system, then messages, and any /// byte change invalidates everything after it. A system prompt carrying a timestamp, a user /// name, or anything else that varies per request will simply never be read back — you pay /// the write premium for nothing. /// /// There is a length floor and it is model-dependent (roughly 1–4k tokens on /// current Anthropic models). A shorter prefix silently does not cache: no error, and /// cache_creation_input_tokens stays at zero. That, and cache_read_input_tokens /// staying at zero across repeated calls, are the two things to look at when it seems not to /// work. /// /// Providers that have no cache concept ignore this flag. /// public bool CacheSystemPrompt { get; init; } } /// Single completion response from a provider. public sealed class LlmResponse { /// Content blocks produced by the assistant in this turn. public required IReadOnlyList Content { get; init; } /// Reason the provider stopped generating. public required LlmStopReason StopReason { get; init; } /// Token usage for this turn. public LlmUsage Usage { get; init; } = LlmUsage.Empty; /// Provider-native raw stop reason for diagnostics. public string? RawStopReason { get; init; } /// /// Opaque backend-configuration fingerprint surfaced by some providers /// (OpenAI's system_fingerprint; xAI / Together echo it; Anthropic /// does not). When two otherwise identical calls return different /// fingerprints, the model was re-released under the same id — the only /// signal an auditor has to detect silent provider drift. /// public string? ProviderSystemFingerprint { get; init; } /// /// Provider-issued response identifier (OpenAI / xAI / Together: top-level /// id; Anthropic: top-level id). Used by auditors to /// cross-reference a persisted row with the provider's own usage / billing /// logs. Null when the provider does not surface one. /// public string? ProviderResponseId { get; init; } } /// /// Single chunk yielded by . Non-final chunks have = /// null and carry the pieces as they arrive: visible text as , the model's thinking as /// without a signature. A piece is partial; the assembled blocks are in /// of the last chunk, which also carries the completed tool calls in . /// public sealed record LlmStreamChunk( IReadOnlyList Content, LlmStopReason? StopReason, LlmUsage? Usage) { /// /// The whole answer assembled from the stream, exactly what returns for /// the same answer: blocks in the order they arrived, thinking with its signature, tool calls, usage, the stop /// reason and its raw value, the response id. Set on the last chunk only. /// public LlmResponse? Response { get; init; } }