Tokens count #1112
Replies: 6 comments
-
|
You could use callbacks now, but I want to add a better way to report on it adding this as feature accepted |
Beta Was this translation helpful? Give feedback.
-
|
@joaomdmoura Any update on this feature? Need this. btw, Awesome job! |
Beta Was this translation helpful? Give feedback.
-
|
crew = Crew( print(crew.usage_metrics) |
Beta Was this translation helpful? Give feedback.
-
|
I believe I found a bug in the By ensuring that only a single instance of I've opened a PR for this, but would like some help verifying this hypothesis please. Thanks everyone! Here's the modified method: @model_validator(mode="after")
def set_agent_executor(self) -> "Agent":
"""set agent executor is set."""
if hasattr(self.llm, "model_name"):
token_handler = TokenCalcHandler(self.llm.model_name, self._token_process)
# Ensure self.llm.callbacks is a list
if not isinstance(self.llm.callbacks, list):
self.llm.callbacks = []
# Check if an instance of TokenCalcHandler already exists in the list
if not any(isinstance(handler, TokenCalcHandler) for handler in self.llm.callbacks):
self.llm.callbacks.append(token_handler)
if not self.agent_executor:
if not self.cache_handler:
self.cache_handler = CacheHandler()
self.set_cache_handler(self.cache_handler)
return self |
Beta Was this translation helpful? Give feedback.
-
|
Token counting in multi-agent systems is trickier than it looks — the per-agent view misses the real cost driver, which is how context compounds across delegation chains. A pattern we found useful: three-level quota hierarchy (namespace → user → agent → conversation) with pessimistic pre-allocation. Before any LLM call, deduct the ceiling; credit back the unused portion after. This gives you hard budget floors without requiring accurate upfront estimation. The other thing that helped: model routing by task type rather than using one model for everything. We settled on roughly 58% Haiku-class (fast retrieval, classification, simple tool calls), 31% mid-tier (reasoning + synthesis), 11% full reasoning model (planning, delegation decisions). Blended cost comes out around $3.20/M tokens vs ~$8/M if you run everything on the top model. There's a write-up on the economic model and cost attribution here: https://blog.kinthai.ai/agent-wallet-economic-models-autonomous-agents — covers how to make cost visible to the agent itself so it can make routing decisions rather than just burning budget. What breakdown are you seeing in your token counts — is it context carry-over or actual generation cost that's dominating? |
Beta Was this translation helpful? Give feedback.
-
|
Token counting in CrewAI is important for two related but different purposes: cost visibility (how much did this run cost?) and budget enforcement (stop before this run costs more than $X). For cost visibility: A simple pattern: wrap each agent's LLM calls with a token counter that accumulates into a For budget enforcement: # Pseudocode
def guarded_llm_call(agent, prompt, max_tokens, budget_remaining):
if max_tokens > budget_remaining:
raise BudgetExhausted(f"Need {max_tokens}, have {budget_remaining}")
# Deduct ceiling pessimistically
budget_remaining -= max_tokens
result = llm.call(prompt, max_tokens=max_tokens)
# Credit back unused
actual_used = result.usage.total_tokens
budget_remaining += (max_tokens - actual_used)
return resultCache-hit tokens matter: What's the scale you're trying to track — single task runs or long-running agent deployments? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How to determine tokens usage ?
Beta Was this translation helpful? Give feedback.
All reactions