BoundaryML/baml · error

failed to get input tokens

Error message

failed to get input tokens: %w

What it means

The Go raw-objects usage wrapper calls the underlying 'input_tokens' method on the internal usage object. If that method call fails, the error is wrapped with 'failed to get input tokens: %w'. This indicates the collector's internal object is missing or in an unexpected state.

Solutions

  1. Check the LLM call completed successfully before reading usage
  2. Verify the collector call/stream finished (await stream.Done()) before Usage()
  3. Update baml Go client and runtime to matching versions
  4. Inspect the wrapped error via errors.Unwrap for the root cause

Example fix

// before
tokens, _ := call.Usage().InputTokens()
// after
resp, err := ctx.LlmCall(...)
if err != nil { return err }
tokens, err := resp.Usage().InputTokens()
if err != nil { return err }
Defensive patterns

Strategy: try-catch

Validate before calling

if call == nil || call.Usage() == nil {
    return fmt.Errorf("no usage available")
}

Try / catch

tokens, err := usage.InputTokens()
if err != nil {
    return fmt.Errorf("usage read failed: %w", err)
}

Prevention

When it happens

Trigger: Calling collector/LlmCall Usage().InputTokens() before the LLM call produced usage data, or when the underlying PyO3 raw object does not expose input_tokens.

Common situations: Reading usage from a failed or still-streaming call; mixing collector APIs with raw object wrappers; version drift between the Go client and the embedded runtime.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/86ed5a3f7a9ec0b3. Report an issue: GitHub.

Appendix: source

Thrown at engine/language_client_go/pkg/rawobjects_usage.go:30

	*raw_objects.RawObject
}

func newUsage(ptr int64, rt unsafe.Pointer) Usage {
	return &usage{raw_objects.FromPointer(ptr, rt)}
}

func (u *usage) ObjectType() cffi.BamlObjectType {
	return cffi.BamlObjectType_OBJECT_USAGE
}

func (u *usage) pointer() int64 {
	return u.RawObject.Pointer()
}

func (u *usage) InputTokens() (int64, error) {
	result, err := raw_objects.CallMethod(u, "input_tokens", nil)
	if err != nil {
		return 0, fmt.Errorf("failed to get input tokens: %w", err)
	}

	tokens, ok := result.(int64)
	if !ok {
		return 0, fmt.Errorf("unexpected type for input tokens: %T", result)
	}

	return tokens, nil
}

func (u *usage) OutputTokens() (int64, error) {
	result, err := raw_objects.CallMethod(u, "output_tokens", nil)
	if err != nil {
		return 0, fmt.Errorf("failed to get output tokens: %w", err)
	}

	tokens, ok := result.(int64)
	if !ok {

View on GitHub (pinned to bd85ce9dee)