Back to articles
The 3-Line Python Decorator That Tracks Every Token Your AI Agent Spends

The 3-Line Python Decorator That Tracks Every Token Your AI Agent Spends

via Dev.to PythonVikas Sah

Jensen Huang just told GTC 2026 that every NVIDIA engineer will get a token budget worth half their base salary — $100K-$150K in compute credits. His argument: in the agentic era, your output is capped by your token access, not your working hours. Which means somebody has to track those tokens. Here's a decorator that does it in three lines of logic. The Decorator import functools from collections import defaultdict _token_log = defaultdict ( lambda : { " calls " : 0 , " input " : 0 , " output " : 0 }) _session_total = { " input " : 0 , " output " : 0 } ALERT_THRESHOLD = 500_000 # tokens — adjust to your budget def track_tokens ( fn ): @functools.wraps ( fn ) def wrapper ( * args , ** kwargs ): result = fn ( * args , ** kwargs ) # Extract token counts from the response usage = result . usage # works for OpenAI, Anthropic, LiteLLM inp , out = usage . input_tokens , usage . output_tokens # Log per-function and per-session _token_log [ fn . __name__ ][ " calls " ] += 1 _token_log [ fn . _

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
2 views

Related Articles