Skip to content
Lubo Bali
All projects

AI agentsAny business

AI agents that watch their own cost

Three AI agents that broke in expensive ways, and the fixes that made them cheap and safe

12,000×cheaper per question: $12 down to $0.001

See the code on GitHub (opens in a new tab)

Proof you can check:Code on GitHub

AI agents

The same agents, 12,000× cheaper

  • Cost per question$12$0.00112,000× cheaper
  • AI tokens in one call571K13399.98% less
  • Tools loaded each turn256–11up to 76% fewer

Lubo Bali · lubobali.com

No. 04Every call traced in Langfuse

The problem

AI agents fail quietly and expensively: one dumps a whole database into the AI, one never stops researching, one gets 25 tools and picks the wrong one. The bill is the first sign.

What I built

Three agents with the real fixes: field stripping and size guards on what the AI sees, a budget manager that tracks every step's cost and stops in time, and a keyword router that loads only the tools a question needs.

Tech & why

LangChain with OpenAI, every call traced in Langfuse, so cost, tokens and speed are measured instead of guessed. The router uses no AI at all, so it costs nothing.

  • Python
  • LangChain
  • OpenAI
  • Langfuse
  • MCP

The result

A question went from $12 and 8 minutes to $0.001 and 15 seconds, one call from 571K tokens to 133, and each turn now loads 6–11 tools instead of 25. 46 tests hit the real AI.

How this agent is built

Every AI agent has the same five parts. Here is what each one is in AI agents that watch their own cost, so you can judge any agent the same way.

  1. ModelOpenAI models through LangChain.
  2. HarnessThree LangChain agents; a budget manager tracks every step's tokens and cost and stops the run in time.
  3. GuardrailsOutput size guards, field stripping (no card numbers ever reach the AI), hard step and budget limits.
  4. KnowledgeA support database and research sources, cut down to the fields a question needs.
  5. ToolsMCP tool servers (GitHub, Slack, a database), loaded per question by a keyword router with no AI cost.

Safe by design

No card numbers ever reach the AI (fields stripped first), and every run has a hard budget and step limit.

Similar projects

Have a problem like this? Get your free AI blueprint in about a minute.

Tell me your problem. I'll find a way to build the solution.

Available now

Book a free 30-min call (opens Calendly in a new tab)

No obligation. Pick a time that suits you.

Prefer email?data@lubobali.com