Skip to documentation
Documentation navigation

Documentation navigation

Documentation / guides

API-format estimators

See how the bundled estimator selects a dialect encoder and derives tokens from bytes.

developer

contextcount.Estimator uses the request’s API format to choose one of the bundled request encoders. Its zero value is ready for use.

Selection

switch req.Model.APIFormat {
case model.APIFormatOpenAI:
	body, err = openaiapi.EncodeRequest(req, false)
case model.APIFormatOpenAIResponses:
	body, err = openairesponses.EncodeRequest(req, false)
case model.APIFormatAnthropic:
	body, err = anthropicapi.EncodeRequest(req, false)
case model.APIFormatGemini:
	body, err = geminiapi.EncodeRequest(req)
case model.APIFormatBedrockConverse:
	body, err = bedrockconverse.EncodeRequest(req)
default:
	return nil, &contextcount.UnsupportedAPIFormatError{APIFormat: req.Model.APIFormat}
}

Bedrock Converse is in the bundle: Converse and ConverseStream share one request body, so the encoder takes no mode and the estimator counts exactly what inference sends. A format with no bundled encoder returns a typed UnsupportedAPIFormatError, not a guessed count.

Formula

The estimator counts encoded bytes with a ceiling division by four:

estimatedTokens = ceil(len(encodedRequest) / 4).

The revision constant bundled-openai-responses-anthropic-gemini-bedrock-request-bytes-div4-v3 identifies the encoder suite and formula. A count-affecting codec change requires a new revision so stored measurements remain attributable.

Source and proof

Run go test ./contextcount.

← back to documentation