Skip to content

Ingest Usage with Go

Use client.Ingest from trusted server-side code to submit usage to a published extractor rule set.

Use client.Ingest.Ingest when you want the SDK to marshal a Go payload into raw_json and attach optional Meterry extensions.

resp, err := client.Ingest.Ingest(ctx, "proj_gateway", "ers_openai", sdk.IngestEvent{
Source: "openai-gateway",
ExternalEventID: "chatcmpl_123",
IdempotencyKey: "openai-gateway:chatcmpl_123",
SubjectType: "organization",
SubjectID: "org_001",
OccurredAt: time.Now().Unix(),
RawJSON: map[string]any{
"type": "response",
"provider": "openai",
"usage": map[string]any{
"model": "gpt-4.1-mini",
"prompt_tokens": 1200,
"output_tokens": 300,
},
"meta": map[string]any{
"org_id": "org_001",
"user_id": "user_123",
},
},
XBilling: &types.XBilling{
Items: []types.ReportedChargeItem{{
Metric: "tool_call.web_search",
Quantity: 1,
Unit: "call",
}},
PricingHints: map[string]types.ReportedPriceHint{
"tool_call.web_search": {
UnitPrice: 0.10,
PricingUnit: 1,
Currency: "USD",
},
},
},
PricingRuleSetID: "prs_fallback",
Metadata: map[string]string{
"gateway_region": "us-east-1",
},
})
if err != nil {
return err
}
fmt.Println(resp.RawEventID, resp.Status)

Use client.Ingest.IngestRawEvent if you already have a complete types.IngestEventRequest.

raw := json.RawMessage(`{"provider":"openai","usage":{"prompt_tokens":1200}}`)
resp, err := client.Ingest.IngestRawEvent(ctx, "proj_gateway", "ers_openai", types.IngestEventRequest{
Source: "openai-gateway",
IdempotencyKey: "openai-gateway:chatcmpl_123",
SubjectType: "organization",
SubjectID: "org_001",
OccurredAt: time.Now().Unix(),
RawJSON: raw,
})

SubjectType and SubjectID are optional as a pair. When both are present, they override the extractor subject paths and defaults. When omitted, the extractor resolves path values first and defaults second.

Set XBilling for sparse request-level charge items such as optional tool calls. Price and discount hints are keyed by metric.

event := sdk.IngestEvent{
Source: "agent-runtime",
RawJSON: map[string]any{
"workflow_id": "wf_001",
},
XBilling: &types.XBilling{
Items: []types.ReportedChargeItem{{
Metric: "tool_call.web_search",
Quantity: 2,
Unit: "call",
}},
PricingHints: map[string]types.ReportedPriceHint{
"tool_call.web_search": {UnitPrice: 0.02, PricingUnit: 1, Currency: "USD"},
},
},
}

Set PricingRuleSetID to attach x-pricing-rule to the raw payload. When fallback rating is needed, the event uses that pricing rule set instead of blindly using the currently published tenant default.

event := sdk.IngestEvent{
Source: "openai-gateway",
RawJSON: map[string]any{"provider": "openai"},
PricingRuleSetID: "prs_fallback",
}

sdk.BuildRawJSON can also embed legacy x-billing and x-pricing-rule fields into a raw JSON object when you need that representation explicitly.

Method Description
Ingest Builds an ingest request from sdk.IngestEvent and submits it to a project-scoped extractor rule set.
IngestRawEvent Submits an existing types.IngestEventRequest.
BuildRawJSON Marshals raw JSON and optionally embeds legacy billing or pricing extensions.