惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
I
InfoQ
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
博客园_首页
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
C
Check Point Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
B
Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
F
Fortinet All Blogs
月光博客
月光博客
GbyAI
GbyAI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Built-in Token Counting: Telemetry for Production AI Agents
Elizabeth Fu · 2026-05-13 · via DEV Community
Cover image for Built-in Token Counting: Telemetry for Production AI Agents

Strands Agents provides native telemetry and cost tracking out of the box. Stop writing custom token counters.

Building AI agents is easy. Deploying them to production is where most teams hit a wall.

One of the first questions from finance: "How much will this cost per request?"

Most agent frameworks make you build your own token counter. Strands Agents gives you one.

The Problem with Custom Token Counting

Every AI application needs cost monitoring. But tracking tokens across:

  • Multiple model calls
  • Tool invocations
  • Prompt caching
  • Multi-agent workflows

...requires custom infrastructure most teams rebuild from scratch.

Native Telemetry in Strands Agents

Strands Agents includes production-grade telemetry by default:

from strands import Agent
from strands_tools import calculator

# Create an agent with tools
agent = Agent(tools=[calculator])

# Invoke the agent with a prompt and get an AgentResult
result = agent("What is the square root of 144?")

# Access metrics through the AgentResult
print(f"Total tokens: {result.metrics.accumulated_usage['totalTokens']}")
print(f"Execution time: {sum(result.metrics.cycle_durations):.2f} seconds")
print(f"Tools used: {list(result.metrics.tool_metrics.keys())}")

# Cache metrics (when available)
if 'cacheReadInputTokens' in result.metrics.accumulated_usage:
    print(f"Cache read tokens: {result.metrics.accumulated_usage['cacheReadInputTokens']}")
if 'cacheWriteInputTokens' in result.metrics.accumulated_usage:
    print(f"Cache write tokens: {result.metrics.accumulated_usage['cacheWriteInputTokens']}")

Enter fullscreen mode Exit fullscreen mode

No configuration. No custom code. It just works.

What You Get

Every AgentResult includes:

Metric Description
inputTokens Tokens sent to the model
outputTokens Tokens generated by the model
totalTokens Total cost (input + output)
cacheReadInputTokens Tokens read from cache (Bedrock prompt caching)
cacheWriteInputTokens Tokens written to cache

Multi-Agent Token Tracking

For multi-agent systems (executor → validator → critic), aggregate metrics across all agents:

from strands.multiagent import Swarm

swarm = Swarm([executor, validator, critic])
result = swarm("Query")

total_tokens = 0
for node_result in result.results.values():
    usage = node_result.result.metrics.accumulated_usage
    total_tokens += usage['totalTokens']

print(f"Total cost across all agents: {total_tokens} tokens")

Enter fullscreen mode Exit fullscreen mode

Per-Cycle Tracking

For agents that run multiple reasoning cycles, track tokens per cycle:

from strands import Agent
from strands_tools import calculator

agent = Agent(tools=[calculator])

# First invocation
result1 = agent("What is 5 + 3?")

# Second invocation
result2 = agent("What is the square root of 144?")

# Access metrics for the latest invocation
latest_invocation = result2.metrics.latest_agent_invocation
cycles = latest_invocation.cycles
usage = latest_invocation.usage

# Or access all invocations
for invocation in response.metrics.agent_invocations:
    print(f"Invocation usage: {invocation.usage}")
    for cycle in invocation.cycles:
        print(f"  Cycle {cycle.event_loop_cycle_id}: {cycle.usage}")

# Or print the summary (includes all invocations)
print(result2.metrics.get_summary())

Enter fullscreen mode Exit fullscreen mode

For a complete list of attributes and their types, see the EventLoopMetrics API reference.

Why This Matters

Cost visibility is the difference between a prototype and production AI.

Use Cases

With Strands telemetry:

  • ✅ Budget AI workloads before deployment
  • ✅ Identify expensive queries in production
  • ✅ Optimize prompts with real token data
  • ✅ Track prompt caching savings

All without writing a single line of telemetry code.

Works with All Model Providers

Token tracking works regardless of your model provider:

  • Amazon Bedrock (Claude, Llama, Mistral)
  • OpenAI (GPT-4, GPT-3.5)
  • Anthropic API
  • Ollama (local models)

Same API, same metrics, zero config changes.

Try It

pip install strands-agents

Enter fullscreen mode Exit fullscreen mode

Full documentation: strandsagents.com/docs/user-guide/concepts/agents/


Gracias!

🇻🇪🇨🇱 Dev.to Linkedin GitHub Twitter Instagram Youtube