Documentation
¶
Overview ¶
Package thinkcount keeps the token count of each thinking block a model made, so the accountant can price a block at its own count when the client sends it back (accountant design D16, issue #1187).
A response reports one figure for its reasoning: usage.output_tokens_details.thinking_tokens (Anthropic). A host reads that figure and the signature of each new thinking block from the response (FromBody, or a Collector on a stream), and records them in a Table. On the next request the host builds its bodies with accountant.NewBodyCounted(..., table.Lookup).
Data dependence: metadata only. The table holds a 64-bit hash of each signature and an integer. A signature is read only to hash it and take its length; neither the signature nor any text is kept.
Index ¶
Constants ¶
const DefaultMax = 20000
DefaultMax is the number of blocks a Table keeps. A Claude Code turn makes about one block, so this is some thousands of turns across all sessions. An entry costs about 30 bytes in memory and in the state file.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector reads a response as it goes by: Write takes each chunk as the proxy forwards it. On an Anthropic SSE stream it keeps only the signature of each thinking block (from content_block_start and its signature_delta events) and the last thinking_tokens a message_delta or message_start reported. Its zero value reads a stream. NewCollector picks the mode from the content type: any other body is buffered, up to 8 MiB, and read as JSON at the end. It is not safe for concurrent use.
func NewCollector ¶
NewCollector makes a Collector for a response of this content type.
type Response ¶
Response is what one response says about its thinking blocks: the opaque string of each new block (a thinking signature, or the data of a redacted_thinking block), in order, and the response's thinking tokens. HasTokens is false when the usage did not report the figure.
func FromBody ¶
FromBody reads one response: an Anthropic Messages JSON body, or its SSE stream when contentType is an event stream. Any other body gives an empty Response.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a bounded map from accountant.OpaqueKey to a token count. It is safe for concurrent use. When it is full, the oldest entry goes first.
func (*Table) Lookup ¶
Lookup is the accountant.OpaqueCount of the table. Nil-safe: a nil Table knows no count.
func (*Table) MarshalJSON ¶
MarshalJSON writes the entries oldest first, as [key, tokens] pairs. A key is a decimal string, so a JavaScript reader does not round it.
func (*Table) Record ¶
Record stores the counts of one response: its thinking tokens split between its blocks by length (accountant.SplitOpaqueTokens). It does nothing for a response with no count. Nil-safe.
func (*Table) UnmarshalJSON ¶
UnmarshalJSON adds the entries MarshalJSON wrote, oldest first, so the newest survive when the table is smaller than the file. A malformed entry fails the whole read and leaves the table unchanged. A negative count is skipped.