How We Processed 13 Billion Tokens at R$ 0.04 Per Million

@gmprestes
โปรตุเกส1 วันที่ผ่านมา · 09 ก.ค. 2569
274K
11
0
1
3

TL;DR

Guilherme Silva details the architecture of Velua Code, an AI agent that uses context compression, model routing, and knowledge graphs to process billions of tokens at a fraction of standard costs.

In the last month, the coding agents I am building processed 13 billion tokens between input and output, with a 97.24% cache hit rate and an effective cost of approximately R$ 0.04 per million tokens.

When I mention these numbers, the first reaction is usually distrust—and that is healthy. Cost is the number one reason companies give up on autonomous agents: the pilot works, the bill arrives, the project dies. So this article is about how these numbers happen. There is no single trick; there is an architecture where every token that enters and exits is compressed, routed, and measured.

This is Velua Code, the agent we are building at a new startup I decided to start, Velua AI (https://velua.ai). Before the architecture, the thesis.

## The Thesis: Three Problems, Solved Together

Coding agents break in three places, and solving only one is not enough.

Cost. An autonomous agent consumes tokens at a scale that scares any CFO. If each iteration is expensive, no one lets the agent iterate—and an agent that doesn't iterate doesn't really solve anything.

Context. The context window is finite and expensive. The typical implementation fills the prompt with entire files and grep results, paying for thousands of irrelevant tokens in every call.

Memory. Every session starts from scratch. The agent rediscovers on Tuesday what it had already learned on Monday—and pays (in tokens and errors) to rediscover it.

The three feed into each other: inflated context increases cost, lack of memory inflates context. That's why Velua Code attacks all three at once.

## Cost: Source Compression + Active Routing

The first architectural decision: compress context at the source, not at the end. Every tool output—file reading, search results, build logs—goes through a compression pipeline before entering the session history. The core is a proprietary compression model running locally in ONNX, on the dev's machine or in the agent's container. There is no network call to compress: the savings don't cost tokens.

Around it, simpler layers do the heavy lifting: deduplication of reads (did the agent reread the same file? the old version leaves the context), structural JSON compression, code body elision while maintaining signatures, and an adaptive threshold that tightens compression as the context grows. Everything is measured with the target model's actual tokenizer—savings counted in real tokens, not estimates.

And there is a decision on what not to do: we never touch the system prompt at runtime. A stable prompt is what sustains the 97.24% cache hit rate—and cache hit is the cheapest cost lever that exists, because a cached token costs a fraction of a full token.

The second decision: the agent does not choose the model. A local classifier pre-classifies each task by category and complexity, and the Velua Gateway—which sees 50+ models with real-time price and performance—routes to the most capable model within what is necessary, in addition to applying guardrails and enriching with RAG. Renaming a variable doesn't need a frontier model; designing a schema migration does. With active routing, most calls go to smaller models, and the expensive one only comes in when complexity demands it.

The gateway also measures the real cost of each request. This allows for something I consider non-negotiable for agents in production: budget as a stopping condition. The autonomous loop runs with a cost ceiling in currency, not with hope.

It is the combination—source compression, high cache, routing between smaller models—that produces the R$ 0.04 per million. None of the three pieces alone comes close.

## Context: A Graph Instead of Grep

The standard way for an agent to "understand" a codebase is grep and file reading—expensive and blind. Velua Code maintains a code knowledge graph: functions, classes, routes, and the relationships between them (who calls whom, who implements what).

This changes both ends of the loop. On input, the agent assembles a lean context package by consulting the graph—the project's architectural view and nodes relevant to the task—instead of dumping files into the prompt. On output, it changes verification: when the agent modifies a function, the graph lists exactly the impacted call points, and a reviewer agent—with clean context, without the bias of the one who wrote the code—checks each of them, in addition to running tests, lint, and build. "You changed the signature of processOrder; seven places call it" is a type of verification that grep doesn't deliver.

## Memory: The Loop That Learns

The piece that closes the system. At the end of each verified iteration, the agent records engineering decisions: what was decided, why, what alternatives were considered, what failed. And each decision is linked to the code nodes it explains, within the graph itself.

In the next iteration, the context collection phase retrieves these decisions—including approaches that have already failed, so as not to repeat them. The loop stops being an executor that repeats tasks and becomes a system that accumulates knowledge about the codebase. It is also the best cost amortizer that exists: cheap memory replaces expensive rediscovery.

The complete loop, then: collect context (graph + memory + RAG), plan with the right model for the problem size, act with sub-agents, verify with a clean-context reviewer and graph awareness, learn by recording decisions—and repeat, with a cost ceiling. It is the canonical agent loop, with each generic phase replaced by its own capability.

## Why the First Client is Us

The product strategy is deliberately counterintuitive: before selling to any client, Velua Code runs internally at SIGE Cloud. Real dogfooding—an ERP in production, with real teams operating agents on real code, every day.

It was this internal use that generated the 13 billion tokens, and it is what is shaping the product. Autonomous agents in production expose problems that no benchmark exposes: permissions, accumulated cost, tasks that stall, context that rots. I prefer it to mature where the pain is ours.

## What Comes Next: Unified Memory for Companies

Today, decision memory lives per project. The next step is what excites me most: elevating it to a unified engineering memory layer for companies.

Imagine the decision recorded by the agent of team A—"we migrated to X because of Y; we avoided Z because it broke W"—retrievable by the agent of team B, and by the human devs who operate these agents, with access control, provenance, and auditing. The question "why is this code like this?" answered with the original decision, linked to the code, for anyone or any agent in the organization. Faster onboarding, consistency between teams, and the company's engineering knowledge no longer living only in people's heads.

Served by the gateway, this memory becomes infrastructure: any agent in the company, in any tool, inherits the accumulated learning.

> Autonomous agents will be a commodity. The knowledge they accumulate about
>
>
> your
>
>
> system will not.

That is the bet.

If you are building with agents in production—or banging your head against context costs—my DM is open.

บันทึกในคลิกเดียว

อ่านบทความไวรัลเชิงลึกด้วย AI ใน YouMind

Save the source, ask focused questions, summarize the argument, and turn a viral article into reusable notes in one AI workspace.

Explore YouMind
สำหรับครีเอเตอร์

เปลี่ยน Markdown ของคุณให้เป็นบทความ 𝕏 ที่สะอาดตา

เวลาคุณเผยแพร่งานเขียนยาวของตัวเอง การจัดรูปแบบรูปภาพ ตาราง และบล็อกโค้ดให้เข้ากับ 𝕏 นั้นน่าปวดหัว YouMind เปลี่ยนร่าง Markdown ทั้งฉบับให้เป็นบทความ 𝕏 ที่สะอาดตาและพร้อมโพสต์ทันที

ลอง Markdown เป็น 𝕏

แพตเทิร์นให้ถอดรหัสเพิ่มเติม

บทความไวรัลล่าสุด

สำรวจบทความไวรัลเพิ่มเติม