accwire

package
v0.7.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package accwire reads a request body for the accountant, one adapter per wire.

The accountant library is wire-agnostic: it takes a Body of hashed positions, cache-mark breakpoints and operations. This package turns a provider request into those. Each wire reads its own shape in four places, and they all sit behind one Wire value, chosen once per body:

  • Body and Parse cut a body into tools, system and messages (the hash space of the positions);
  • Breakpoints finds the cache marks;
  • EnvelopeOps and MessageOps report the edits that no pipeline component reports;
  • WireIndex maps a pipeline slot path to the wire position it sits in.

A new wire (accountant#17) adds one kind here, and no other place. The package is a library: it imports no host package (proxy, proxyapp, dash).

The write side stays out. apply's slot types rewrite the request that goes upstream, so only this read side sits behind the interface (#1164).

Index

Constants

View Source
const (
	// OpKeeper is accountant.PingOps's operator: the keep-alive ping's spend. It has no host prefix.
	// The library exports no constant for it; TestOpKeeperIsThePingOperator reads it from PingOps.
	OpKeeper = "keeper"
	// OpHeadTTL is the 1h head TTL, a host option (apply/headttl.go, envelopeOps).
	OpHeadTTL = "headttl"
	// OpTailBreakpoint is the trailing cache mark apply adds (apply/ops.go).
	OpTailBreakpoint = "tailbreakpoint"
	// OpToolDefer is tool deferral (accrec's billed-body pass, the proxy's MessageOps).
	OpToolDefer = "tooldefer"
	// OpExpand and OpAdjudicate are the tool declarations expand and adjudicate inject (envelopeOwner).
	OpExpand     = "expand"
	OpAdjudicate = "adjudicate"
	// OpExpandRestore is the copy fixed_restore inserts (apply/restore.go, #481).
	OpExpandRestore = "expand_restore"
)

The operators that book savings but are not registered components (components.Names()). Each emit site uses its constant, so OperatorsOutsideRegistry cannot miss a name the code books.

View Source
const OperatorPrefix = "context-guru."

OperatorPrefix is the host prefix of every operator name context-guru books. The accountant's metrics sink cuts it off, so the `component` label is the bare name.

View Source
const (
	// SpendParamEstimated is "true" when the usage is our estimate, not a count the provider billed. It documents the
	// call on the wire only: accountant.Figure marks its figure estimated from the pricer, not from Params.
	SpendParamEstimated = "estimated"
)

The Params keys of a spend operation the host prices from its own Call (tool deferral's search turns, #1441).

Variables

This section is empty.

Functions

func FindCacheMark

func FindCacheMark(v gjson.Result, prefix string) string

FindCacheMark returns the gjson path of the first cache-control mark under v, or "" when there is none. A mark is a cache_control key on v itself or on a block of a content array, at any depth of content nesting (a tool_result block's own content). It goes down only through "content" arrays, so a cache_control key anywhere else stays in the hash: a tool's input_schema.properties.cache_control parameter, or a tool_use block's input value, is part of what the provider caches, and a change to it must change the hash (#1272). It never looks inside a string.

func Operator added in v0.7.9

func Operator(name string) string

Operator returns the booked operator name of name: the host prefix and the bare name.

func OperatorsOutsideRegistry added in v0.7.9

func OperatorsOutsideRegistry() []string

OperatorsOutsideRegistry lists every operator that books savings and is not a registered component. A metrics sink built with WithComponents(components.Names()...) pushes these as component="other"; accrec.MetricComponents adds them.

func WithoutCacheMarks

func WithoutCacheMarks(body []byte, v gjson.Result) []byte

WithoutCacheMarks is one element's JSON without its cache_control marks: the element's own, the ones on its content blocks, and the ones nested deeper in content, such as a mark inside a tool_result block's own content array (issue #917). A key named cache_control anywhere else is data, not a mark, and stays (see FindCacheMark). An element with none is returned as it is, byte for byte, so the common case costs no allocation. The result is only hashed. It is never sent anywhere.

It is not schema's dropCacheControl, on purpose. That one decodes and re-encodes a message, drops cache_control at every depth and sorts the keys, to compare two messages by meaning. This one keeps the wire bytes and removes only the marks the provider ignores (#1272), so the hash and the sizes stay those of the wire.

Types

type Wire

type Wire interface {
	// Name is "messages" or "responses".
	Name() string
	// Body hashes a request body in the provider's order: tools, system, messages. A body without a
	// conversation (or with invalid JSON) gives an empty Body.
	Body(raw []byte) accountant.Body
	// Parse is Body and Breakpoints on one parse of the body. count prices each thinking block the
	// proxy saw a model make (accountant design D16); nil gives every block the fixed price. The
	// hashes and sizes do not depend on it.
	Parse(raw []byte, count accountant.OpaqueCount) (accountant.Body, []accountant.Breakpoint)
	// Breakpoints lists the cache marks as positions in the same space as Body.
	Breakpoints(raw []byte) []accountant.Breakpoint
	// EnvelopeOps lists the operations that explain the tools and system sections' difference
	// between the client body and the forwarded body. It is nil when either has no conversation.
	EnvelopeOps(client, forwarded []byte) []accountant.Operation
	// MessageOps explains a change to the messages that an edit made AFTER the pipeline, with the
	// edit's own operator.
	MessageOps(operator string, before, after []byte) []accountant.Operation
	// WireIndex maps a normalized slot path, such as "messages.3.content.1.text" or
	// "input.3.content.1.text", to the wire position it sits in. ok is false when the path names
	// no position of this wire.
	WireIndex(slotPath string) (index int, ok bool)
}

Wire is the accountant's reader for one provider wire.

var (
	// Messages reads Anthropic Messages and Chat Completions bodies.
	Messages Wire = messages
	// Responses reads OpenAI Responses bodies.
	Responses Wire = responses
)

func For

func For(raw []byte) Wire

For chooses the wire of a request body, once. A body with no messages array and an input (an array of items, or one string) is Responses. Every other body is Messages, which gives an empty Body when there is no conversation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL