You’ve done it. After days of prompt engineering, wrestling with LangChain, and debugging esoteric errors, your AI agent finally works. It autonomously researches topics, uses tools, and completes the complex task you assigned--it feels like magic. Then you open your OpenAI billing dashboard and the magic vanishes, replaced by a cold, hard, three-digit number that’s growing way too fast.
I’ve been there. The very nature of agentic workflows--with their unpredictable loops and chains of thought--turns cost forecasting into a complete guessing game. This is a massive problem, not just for engineers, but for anyone trying to build a viable product on top of this technology.
That’s why I built agent-cost-tracker. It’s an open-source Python library that gives you a crystal-clear, step-by-step breakdown of your AI agent's API costs. It tracks every call, calculates the expense for both input and output tokens, and generates an interactive visualization so you can see exactly where your money is going. No more billing surprises, just the data you need to build efficient and economically viable agents.
Quick Start
Getting started is trivial. You wrap your agent's execution code in a CostTracker context manager. That's it. It automatically patches the necessary libraries and starts listening.
Here’s a complete example. Let's assume you have your agent's logic in a function called run_my_agent_flow.
from agent_cost_tracker import CostTracker
from my_agent_module import run_my_agent_flow # Your agent code lives here
# Initialize the tracker
with CostTracker() as cost_tracker:
# Run your agent as you normally would
run_my_agent_flow("What were the key highlights of Apple's latest earnings call?")
# Print the total cost and generate an interactive report
print(f"Total cost for the run: ${cost_tracker.get_total_cost():.4f}")
cost_tracker.visualize_costs(browser=True) # Opens a report in your browser
After running this, you'll get a beautiful, interactive HTML file that breaks down the cost of every single LLM call your agent made during that run.
How It Works
The magic behind agent-cost-tracker is a technique called monkey-patching. When you enter the with CostTracker() as ... block, the library temporarily replaces the API call methods from popular libraries like openai and litellm with its own custom versions. Don't worry--it's less chaotic than it sounds.
Here's the sequence:
- Patching:
CostTrackerfinds thechat.completions.createandchat.completions.acreatemethods in theopenaiclient object. It stores a reference to the original methods and puts its own "wrapper" method in their place. - Execution: Your agent code runs exactly as written. It thinks it's calling the normal OpenAI API, but it's actually calling the
CostTrackerwrapper. - Interception: The wrapper first calls the original OpenAI method, letting the API call complete successfully. When it receives the response, it intercepts it before passing it back to your agent. It pulls the
usageobject from the response payload, which contains theprompt_tokensandcompletion_tokens. - Calculation & Logging: Using a built-in, up-to-date price list for different models (like
gpt-4-turbo,claude-3-opus, etc.), the tracker calculates the precise cost of that individual call. It logs the model used, the token counts, and the final cost, then returns the original response to your agent so the workflow can continue uninterrupted. - Restoration: As soon as your code exits the
withblock--either by finishing or by raising an error--the original, un-patched methods are put back in their place. This ensures the tracker has zero side effects on any other part of your application.
The final step, visualization, is handled by Plotly. It takes the logged data and generates a self-contained HTML file with an interactive Sankey diagram. This diagram is perfect for visualizing flows, letting you easily trace the path of your agent and see which steps or tool uses are racking up the biggest bill.
Why I Built This
I’m a Program Manager with a background in Business Operations, and I'm obsessed with agentic AI. My job is to bridge the gap between business strategy and AI engineering. I don’t just write strategy decks; I build real tools to prove what’s possible and uncover the operational hurdles we’ll face in production.
In BizOps, you learn one thing very quickly: a project without a predictable budget is a non-starter. When I started building complex AI agents, I was horrified by how opaque their costs were. An agent designed to do research might make five API calls for one query and fifty for another. You can't build a business on that kind of variance without a way to measure and control it.
I needed answers to basic business questions:
- What is our average cost per task?
- Which tool is the most expensive for our agent to use?
- If we swap
gpt-4-turboforclaude-3-sonnetin a specific step, how much do we save, and what's the impact on quality?
agent-cost-tracker is the tool I needed to answer those questions. It turns an engineering black box into a measurable business process. It provides the concrete data required to make informed trade-offs between cost, latency, and performance. This is the same philosophy I applied to my other project, llm-sycophancy-eval, which stress-tests agents for behavioral flaws. First, you have to understand and measure the system--whether its behavior or its cost--before you can optimize it.
What's Next
This is just the beginning. I believe cost-awareness needs to be a first-class citizen in the agent development lifecycle. Here are a few things I have planned:
- Expanded Provider Support: Adding first-class support for other major model providers like Cohere, Gemini (through their native SDKs), and more open-source models.
- Budget Thresholds and Alerting: The ability to set a maximum budget for a run. If the agent exceeds it, the tracker will raise an exception to halt execution, preventing runaway costs.
- Deeper Dashboard Insights: More advanced analytics in the visualization, like breaking down costs by the "tool" being used or providing time-series data to spot performance regressions.
The project is fully open-source, and I welcome contributions. If you have an idea or want to help build out these features, please open an issue or a pull request on GitHub.
Links
- GitHub Repo: https://github.com/manishrawal95/agent-cost-tracker
- Chat with me: Have ideas or want to talk about agentic AI? Book a call.
- Connect on LinkedIn: https://linkedin.com/in/manishrawal95
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.