Documentation
¶
Index ¶
- Constants
- func ApplyServiceTier(cost float64, t ServiceTier) float64
- func BillingModel(requested *core.Model, served string, lookup func(string) *core.Model) (m *core.Model, billed string)
- func ComputeCost(m *core.Model, u core.Usage) float64
- func ToolResultText(v core.ToolResultMessage) string
- type DeferredSplit
- type Marshaller
- type PairingReport
- type ServiceTier
- type SyncReport
- type ToolPrefix
Constants ¶
const SyntheticResultText = "No result provided"
SyntheticResultText is the content of the tool result RepairToolPairing invents for a call nothing answered. It is model-visible, so it is pinned here rather than formatted at the call site.
Variables ¶
This section is empty.
Functions ¶
func ApplyServiceTier ¶
func ApplyServiceTier(cost float64, t ServiceTier) float64
ApplyServiceTier applies a service tier's multiplier to a computed cost. It is a separate call rather than a parameter of ComputeCost because the multiplier applies POST-HOC to the whole computed cost, cache reads and writes included, and the tier that applies is the one the response says served it, known only once the response is in.
func BillingModel ¶
func BillingModel(requested *core.Model, served string, lookup func(string) *core.Model) (m *core.Model, billed string)
BillingModel picks the model to bill: the one that actually SERVED the request.
served is the model id the response named. When it differs from the requested id, lookup resolves the served model's catalog row and that row's prices are used; billed reports what to record in Usage.BilledModel. When a later event names the requested model again, calling this again with that id reprices BACK — which is why cost is computed once at the end of a stream from the final name, never incrementally per event.
lookup may return nil for an unknown served model. That case bills at the REQUESTED model's rates and still records the served name, which is the least-wrong option: the alternative is billing a fallback-served response at zero.
func ComputeCost ¶
ComputeCost prices a usage against a model's catalog row. Rates are USD per 1M tokens.
Two subtleties:
InputTokens must already be NET of cache reads and writes. This function does not subtract, because it cannot tell a net value from a gross one, and subtracting an already-net value understates cost as silently as not subtracting a gross one overstates it. The netting is the DECODER's job, at the one place that knows the wire convention: the OpenAI family reports prompt_tokens inclusive of cached tokens and must subtract; Anthropic reports input_tokens exclusive of both and must not.
1h cache writes bill at 2x BASE INPUT, not at the cache-write rate. CacheWrite1hTokens is a SUBSET of CacheWriteTokens, so the 5-minute portion is the difference. Treating them as separate addends double-counts every 1h write.
func ToolResultText ¶
func ToolResultText(v core.ToolResultMessage) string
ToolResultText renders a tool result as the single string a wire with no is_error flag carries — the OpenAI Chat Completions and Responses wires. An error result is marked in the text, because the model is the only consumer that can act on it and the wire gives it nothing else to tell a failed tool from one that printed the same words. It is the rule the Ollama adapter applies for the same reason.
Types ¶
type DeferredSplit ¶
type DeferredSplit struct {
Immediate []core.ToolWire
Deferred []core.ToolWire
// Promoted is true when the safety valve fired: every tool would have been
// deferred, leaving no prefix to anchor against, so all were promoted back
// and the cache wipe accepted.
Promoted bool
}
DeferredSplit is SplitDeferredTools' partition.
func SplitDeferredTools ¶
func SplitDeferredTools(tools []core.ToolWire, history core.Messages) DeferredSplit
SplitDeferredTools splits tools into the immediate ones, which form the cached prefix, and the deferred ones, declared after it.
A tool is deferred only if a tool result MARKED it as newly added and no assistant turn used it BEFORE that marker. The pass is forward and the decision is made at the marker: later usage cannot un-defer a tool.
That ordering is deliberate and it is not obvious. A tool used after being added is exactly the normal case — a skill activates and the model calls its tool on the next turn — and un-deferring on later use would promote every deferred tool on the turn after it appeared, which is when promoting it is most expensive: the transcript is longest and the cache prefix most valuable.
func (DeferredSplit) IsDeferred ¶
func (s DeferredSplit) IsDeferred(name string) bool
IsDeferred reports whether a named tool is in the deferred set.
type Marshaller ¶
type Marshaller func(*schema.Schema) (json.RawMessage, error)
Marshaller renders one tool's schema into its provider's dialect. It is a parameter rather than a fixed json.Marshal because the dialects differ: Gemini strips keywords and upper-cases types, and a cache keyed on the canonical form would hand a provider the wrong bytes.
type PairingReport ¶
type PairingReport struct {
// DroppedAssistants counts assistant messages that ended in an error or
// were aborted.
DroppedAssistants int
// DroppedResults counts tool results that answered no call of the turn
// they sit in: the results of a dropped turn, results of an unknown call,
// and a second result for one call.
DroppedResults int
// Inserted counts the synthetic results added for unanswered calls.
Inserted int
}
PairingReport records what RepairToolPairing changed. It is returned rather than logged, so a caller diagnosing a 400 can tell whether the transcript it sent is the transcript it holds.
func RepairToolPairing ¶
func RepairToolPairing(in core.Messages) (core.Messages, PairingReport)
RepairToolPairing is the transcript repair shared by the chat-style wires (OpenRouter and Ollama Chat Completions): wires that pair a tool call with its result by id in separate messages, and reject a call that has none.
It returns a repaired view and never changes its input, its messages or their content. It applies three rules:
- An assistant message that ended in an error or was aborted is dropped.
- A tool result is kept only in the run of results that directly follows the assistant message that made the call. A result of a dropped turn, of an unknown call, one that follows no assistant message, and a second result for the same call are dropped.
- A call of a kept assistant message with no result in its run gets an error result with the text SyntheticResultText. The run is then ordered as the calls are, so a wire that pairs by position pairs correctly; a run that needs no synthetic result is left as it was recorded.
The result is a fixed point: repairing it again changes nothing. Unlike the Anthropic repair it never touches thinking, which a chat-style wire replays on its own terms.
func (PairingReport) Changed ¶
func (r PairingReport) Changed() bool
Changed reports whether the repair altered the transcript.
func (PairingReport) String ¶
func (r PairingReport) String() string
type ServiceTier ¶
ServiceTier is a service tier's post-hoc cost multiplier.
func KnownServiceTier ¶
func KnownServiceTier(name string) (ServiceTier, bool)
KnownServiceTier resolves a tier NAME a response reported to its multiplier: flex 0.5x, priority 2x, and the standard tier 1x.
It exists for the served-tier case: a flex request can be served at standard, and the response says so in service_tier. Pricing with the CONFIGURED tier then halves a bill the vendor charged in full, silently. The lookup is by name because the configured ServiceTier carries only the name the caller asked for and the multiplier for that one name.
type SyncReport ¶
type SyncReport struct {
// Added names tools that appeared since the last Sync. An addition does
// NOT invalidate.
Added []string
// Invalidated is true when a tool was REMOVED or an existing tool's schema
// CHANGED. Either rewrites the cached prefix and costs the whole
// provider-side cache.
Invalidated bool
// Reason names what invalidated it, for the operator who has to explain a
// cost spike.
Reason string
// Marshalled counts schemas actually serialized on this call. Zero on a
// steady-state turn is the whole point of the cache.
Marshalled int
}
SyncReport is what Sync learned, for an embedder's prompt-cache counters.
type ToolPrefix ¶
type ToolPrefix struct {
// contains filtered or unexported fields
}
ToolPrefix is the per-session serialized tool list.
It is a value on the session, never a package-level map: two agents in one process routinely hold tools of the same NAME and different schemas, and a shared cache keyed by name would serve one agent's schema to the other.
func (*ToolPrefix) Sync ¶
func (p *ToolPrefix) Sync(tools []core.ToolWire) ([]json.RawMessage, SyncReport, error)
Sync reconciles against the cached prefix using plain JSON Schema.
func (*ToolPrefix) SyncWith ¶
func (p *ToolPrefix) SyncWith(tools []core.ToolWire, marshal Marshaller) ([]json.RawMessage, SyncReport, error)
SyncWith reconciles the current tool list against the cached prefix and returns the serialized schemas, in the provider's own dialect, in order.
A nil receiver is a working no-op that marshals every time. That is what lets a provider hold an optional prefix without branching at the call site, and what keeps BuildRequest usable from a test with no session behind it.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package anthropic implements the Anthropic Messages wire API.
|
Package anthropic implements the Anthropic Messages wire API. |
|
Package faux is a deterministic provider that replays a scripted sequence of assistant turns.
|
Package faux is a deterministic provider that replays a scripted sequence of assistant turns. |
|
Package ollama is a core.ProviderClient for a model served by an Ollama server, local or self-hosted.
|
Package ollama is a core.ProviderClient for a model served by an Ollama server, local or self-hosted. |
|
Package openrouter is a core.ProviderClient for a model served by OpenRouter.
|
Package openrouter is a core.ProviderClient for a model served by OpenRouter. |