getsentry / getsentry/sentry-python

Instrument `count_tokens` / `compute_tokens` methods

Open
#5,829 1 comment 0 reactions 0 assignees View on GitHub
Improvement Python Spans
Dominant language
Python
Stars
2.2k
Forks
669
Avg merge
1d 1h
Merged PRs (30d)
213

Description

## Problem

`Models.count_tokens` and `Models.compute_tokens` (and their async variants) make API calls but are not instrumented. They lack spans for latency and error monitoring.

* `count_tokens` works on both Gemini API and Vertex AI, returns `total_tokens` and `cached_content_token_count`
* `compute_tokens` is Vertex AI only, returns actual token IDs and bytes

## Files

* `sentry_sdk/integrations/google_genai/__init__.py` — `setup_once()` needs additional patches

## Acceptance Criteria

* `count_tokens` and `compute_tokens` calls produce spans
* Async variants are also instrumented
* Tests verify span creation and basic attributes

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.