Skip to content

🐛 Bug Report: OPENAI Instrumentation won't record token usage metrics on auto-instrumentation or without tiktoken #2898

@feng-95

Description

@feng-95

Which component is this bug for?

OpenAI Instrumentation

📜 Description

In the pr #661, tiktoken is used to calculate the tokens for prompts and completions. However the enrich_token_usage config is False by default which means it will be False on auto-instrumentation, which means the token usages metrics won't be recorded even the token usage info has been included in response.

Moreover, tiktoken isn't compatible for all models (e.g., our custom models), so the token usages metrics won't be record when using incompatible models.

Given these issues, would it be better to token usage info provided in the response to record usage metrics if the response contains usage information?

👟 Reproduction steps

  1. Create a streaming openai chat. The response contains usage information.
data: {
	"id": "0217465378907901e149xxxx",
	"choices": [],
	"created": 1746537891,
	"model": "custom_model",
	"object": "chat.completion.chunk",
	"service_tier": null,
	"system_fingerprint": null,
	"usage": {
		"completion_tokens": 42,
		"prompt_tokens": 14,
		"total_tokens": 56,
		"completion_tokens_details": {
			"accepted_prediction_tokens": null,
			"audio_tokens": null,
			"reasoning_tokens": 0,
			"rejected_prediction_tokens": null
		},
		"prompt_tokens_details": {
			"audio_tokens": null,
			"cached_tokens": 0
		}
	}
}
  1. Check the token usage metrics

👍 Expected behavior

It can record token usages token metrics if the response include token usages info.

👎 Actual Behavior with Screenshots

It won't record token usage metrics by default.

🤖 Python Version

No response

📃 Provide any additional context for the Bug.

No response

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

Are you willing to submit PR?

Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions