Skip to main content

usage/usage.go (package usage)

Source: sdks/go/usage/usage.go. Back to the Go SDK reference index.

note

This page is generated by tools/docs/gen_sdk_reference.py from the SDK sources and the OpenAPI text folders. Do not edit it by hand: change the source and regenerate.

Class Builder​

Builder assembles a Usage Event.

Builder.At​

At(t time.Time) *Builder

At overrides occurred_at.

NameTypeDefaultRequiredDescription
ttime.TimeyesTime of the consumption, stored in UTC.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Build​

Build() (eai.UsageEvent, error)

Build validates the event against the Usage Event v1 contract.

This element takes no parameter.

Returns: (eai.UsageEvent, error). A UsageEvent: UsageEvent is the Usage Event v1 contract (contracts/v1/usage-event.schema.json). The error is a typed error of the SDK, nil on success.

Raises: error.

Builder.Estimated​

Estimated(amount string, currency string) *Builder

Estimated sets the estimated cost (decimal string).

NameTypeDefaultRequiredDescription
amountstringyesExact decimal amount as a string, for example 0.0042.
currencystringyesISO 4217 currency code, for example EUR.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Execution​

Execution(agentID string, sessionID string, runID string, taskID string) *Builder

Execution sets agent, session, run and task ids.

NameTypeDefaultRequiredDescription
agentIDstringyesAgent that consumed; empty leaves agent_id null.
sessionIDstringyesSession of the consumption; empty leaves session_id null.
runIDstringyesRun that consumed; empty omits run_id.
taskIDstringyesTask of the consumption; empty leaves task_id null.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.ID​

ID(id string) *Builder

ID overrides the event id.

NameTypeDefaultRequiredDescription
idstringyesEvent identifier used instead of the generated ue-<random> one.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Label​

Label(k string, v string) *Builder

Label adds a label.

NameTypeDefaultRequiredDescription
kstringyesName of the label.
vstringyesValue of the label, up to 256 characters.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Lang​

Lang(l string) *Builder

Lang selects the language of validation messages (en, fr).

NameTypeDefaultRequiredDescription
lstringyesLanguage of the Build messages, en or fr.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Measured​

Measured(amount string, currency string) *Builder

Measured sets the measured cost and marks the event measured.

NameTypeDefaultRequiredDescription
amountstringyesExact decimal amount as a string, for example 0.0042.
currencystringyesISO 4217 currency code, for example EUR.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Model​

Model(provider string, modelID string, deploymentID string, region string) *Builder

Model sets provider, model, deployment and region.

NameTypeDefaultRequiredDescription
providerstringyesProvider that served the call; required by the contract.
modelIDstringyesIdentifier of the model that served the call.
deploymentIDstringyesDeployment of the model; empty omits deployment_id.
regionstringyesProcessing region, for example eu-west-1.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Pricing​

Pricing(priceCardID string, version string, currency string, effectiveAt time.Time) *Builder

Pricing sets the price applicable at call time.

NameTypeDefaultRequiredDescription
priceCardIDstringyesPrice card used; empty omits price_card_id.
versionstringyesVersion of the pricing (pricing_version).
currencystringyesISO 4217 currency of the price card.
effectiveAttime.TimeyesTime from which the price applies, stored in UTC.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Project​

Project(projectID string, environment string) *Builder

Project sets project_id and environment.

NameTypeDefaultRequiredDescription
projectIDstringyesProject of the consumption; empty omits project_id.
environmentstringyesEnvironment of the consumption; empty omits environment.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.TokenTotals​

TokenTotals(input int64, output int64, cachedInput int64, reasoning int64) (*Builder, error)

TokenTotals follows the FinOps token rule: input already includes cached, output already includes reasoning (provider totals, the sub counts are never added on top). It returns an error when a sub count exceeds its total.

NameTypeDefaultRequiredDescription
inputint64yesTotal input tokens reported by the provider, cached ones included.
outputint64yesTotal output tokens reported by the provider, reasoning ones included.
cachedInputint64yesCached input tokens, part of input; written as cached_input_tokens when positive.
reasoningint64yesReasoning tokens, part of output.

Returns: (*Builder, error). A Builder: Builder assembles a Usage Event. The error is a typed error of the SDK, nil on success.

Raises: error.

Builder.Tokens​

Tokens(input int64, output int64, reasoning int64) *Builder

Tokens sets input, output and reasoning tokens (zero values are omitted).

NameTypeDefaultRequiredDescription
inputint64yesInput tokens, written as input_tokens when positive.
outputint64yesOutput tokens, written as output_tokens when positive.
reasoningint64yesReasoning tokens, written as reasoning_tokens when positive.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Trace​

Trace(requestID string, providerRequestID string, traceID string) *Builder

Trace sets request, provider request and trace ids.

NameTypeDefaultRequiredDescription
requestIDstringyesPlatform request identifier; empty omits it.
providerRequestIDstringyesRequest identifier given by the provider; empty omits it.
traceIDstringyesTrace identifier; empty omits it.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Builder.Unit​

Unit(name string, value float64) *Builder

Unit sets one unit counter (input_tokens, output_tokens, reasoning_tokens, requests, duration_ms...).

NameTypeDefaultRequiredDescription
namestringyesName of the unit, one of those of the Usage Event contract.
valuefloat64yesConsumed quantity; it replaces a previous value of the same unit.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

Model Options​

Options configures an Emitter.

Fields:

NameTypeRequiredDescription
BatchSizeintyesevents per request, default 100
FlushIntervaltime.Durationyesdefault 2s
QueueSizeintyesbuffered events, default 10000
OnErrorfunc(err error, events []eai.UsageEvent)yesOnError receives batches that failed after the client retries.

Class Emitter​

Emitter sends events in the background. It is safe for concurrent use.

Emitter.Close​

Close(ctx context.Context) error

Close stops accepting events, flushes the queue and stops the loop (call it on shutdown).

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Raises: error.

Emitter.Emit​

Emit(ev eai.UsageEvent) error

Emit queues an event without blocking.

NameTypeDefaultRequiredDescription
eveai.UsageEventyesEvent to send, usually from Builder.Build; it is dropped with ErrQueueFull when the queue is full.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Raises: error.

Emitter.Flush​

Flush(ctx context.Context) error

Flush sends every queued event now and returns the first error.

NameTypeDefaultRequiredDescription
ctxcontext.ContextyesContext of the call: cancellation and deadline.

Returns: error. Error of the call (a typed error of the SDK), nil on success.

Raises: error.

Emitter.Sent​

Sent() int64

Sent returns how many events were accepted by the server.

This element takes no parameter.

Returns: int64. Number of events of the batches the server accepted since the start.

Functions​

NewEvent​

NewEvent(tenantID string, usageType string, source string) *Builder

NewEvent starts an event for a tenant, usage type (llm, embeddings, tool...) and source (gateway, runtime, tool_gateway...). The event id and occurred_at are generated.

NameTypeDefaultRequiredDescription
tenantIDstringyesTenant the consumption is charged to.
usageTypestringyesKind of consumption, for example llm, embeddings or tool.
sourcestringyesEmitting component, for example gateway, runtime or tool_gateway.

Returns: *Builder. A Builder: Builder assembles a Usage Event.

NewEmitter​

NewEmitter(client *eai.Client, opts Options) *Emitter

NewEmitter starts the background loop.

NameTypeDefaultRequiredDescription
client*eai.ClientyesParent client.
optsOptionsyesPer call options (query, idempotency key, traceparent, headers).

Returns: *Emitter. A Emitter: Emitter sends events in the background. It is safe for concurrent use.