thinkcount

package
v0.7.12 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: 8 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 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

View Source
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

func NewCollector(contentType string) *Collector

NewCollector makes a Collector for a response of this content type.

func (*Collector) Result

func (c *Collector) Result() Response

Result is what the stream said. A stream that passed a limit gives an empty Response. A line with no newline at the end of the stream is read too.

func (*Collector) Write

func (c *Collector) Write(p []byte)

Write reads the next bytes of the response. It never fails.

type Response

type Response struct {
	Opaques   []string
	Tokens    int
	HasTokens bool
}

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

func FromBody(contentType string, body []byte) Response

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.

func FromJSON

func FromJSON(body []byte) Response

FromJSON reads an Anthropic Messages response: the "signature" of each "thinking" block and the "data" of each "redacted_thinking" block in content, and usage.output_tokens_details.thinking_tokens.

func (Response) Countable

func (r Response) Countable() bool

Countable says whether the response gives a count to record. A count of 0 is not one: a block with a signature holds some thinking, so 0 means the figure is missing, not that the block is free.

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 NewTable

func NewTable(max int) *Table

NewTable makes a Table that keeps at most max blocks; max <= 0 means DefaultMax.

func (*Table) Len

func (t *Table) Len() int

Len is the number of blocks the table holds. Nil-safe.

func (*Table) Lookup

func (t *Table) Lookup(key uint64) (int, bool)

Lookup is the accountant.OpaqueCount of the table. Nil-safe: a nil Table knows no count.

func (*Table) MarshalJSON

func (t *Table) MarshalJSON() ([]byte, error)

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

func (t *Table) Record(r Response)

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

func (t *Table) UnmarshalJSON(b []byte) error

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.

Jump to

Keyboard shortcuts

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