Cutting tokens without cutting quality: vector search and the right model for every step
Packing prompts full and sending everything to one large model leaves agents slow, costly and less accurate. How vector search reduces tokens, and why focused agents on several models outperform one general agent.
Key takeaways
- Use a vector database to fetch only the relevant context instead of packing the prompt: fewer tokens, faster responses, better answers.
- Mix semantic search with filters and keyword search, and apply the user’s permissions inside the query.
- Already on PostgreSQL? pgvector brings vector search without adding another database.
- Send each step to the smallest model that handles it reliably, and break large agents into focused ones.
Most AI agents start out simple: one large model, one long prompt, and all the context that fits. That is enough for a demo. In production the same agent is slow, costly and, more often than you would expect, less accurate than it should be. Two techniques make a bigger difference than any prompt tuning: fetching only what matters from a vector database, and using several models, each on the job it does best.
Part 1: vector databases, or why less context works better
What goes wrong when you pack the prompt
Models are billed per token, and every token in the prompt is billed again on every call. An agent that sends the whole product catalogue, every policy document or the full list of configuration items “just in case” pays for all of it each time. It also hurts quality: long prompts spread the model’s attention, and the more irrelevant text surrounds the answer, the more likely the model is to miss it or make something up.
How retrieval solves it
A vector database holds embeddings: numeric representations of meaning that an embedding model produces for each document, record or piece of text. When the agent needs context, the question is embedded in the same way and the database returns the few entries closest in meaning. Only those go into the prompt.
- Far fewer tokens. The model gets the few hundred tokens that matter instead of tens of thousands of background ones. Cost and latency fall accordingly.
- Better answers. With focused context the model reasons over the right material and can point to where it came from.
- Grows with your data. The knowledge base can reach millions of entries while the prompt stays the same size.
- Always up to date. Update the index when the data changes and the agent knows straight away. No retraining.
What good retrieval needs
- Sensible chunks. Split documents along their natural structure, such as sections, records or rules, not at arbitrary character counts.
- Meaning plus filters. Similarity alone falls short when the answer must come from one customer, one country or the current year. Good vector stores combine semantic search with ordinary filters, plus keyword search for exact terms like codes and names.
- Permissions in the query. Retrieval may only return what the user behind the agent is allowed to see. Filter by access rights inside the query, not after the model has already read the results.
- A fresh index, re-embedded whenever you switch embedding models: vectors from different models cannot be mixed.
- Retrieval measured on its own. If the right chunk is not among the top results, no model can rescue the answer.
You may not need another database at all. If you already run PostgreSQL, the open-source pgvector extension adds vector search right next to your existing data, with the transactions, filters and backups you already know. Dedicated vector databases pay off at very large scale, but many teams never reach it. (More on how far Postgres can go in our Postgres post.)
Part 2: several models, one job each
One large model is an expensive way to do simple things
A single top-tier model can classify a request, pull out a date, summarise a document and plan a complex change. Most of those steps do not need its power, yet every call pays for it. It is like asking your most senior architect to sort the post.
Send each step to the right model
A focused agent breaks the work into steps and hands each one to the smallest model that does it reliably:
- Small, fast models for classification, routing, extraction and checking that output has the right shape. They are cheap and reply in milliseconds.
- Embedding models for search and similarity, as described above. Cheaper still.
- Mid-sized models for most everyday reasoning: answering questions over retrieved context, drafting and summarising.
- The largest models only where they are worth the cost: multi-step planning, hard reasoning, code and the decisions that matter most.
A common setup puts a small model in front to decide what kind of request it is, retrieval to gather the right context, and then either a mid-sized model that answers directly or a large model that plans, with small models checking the result. Every step has a narrow prompt and a clear job, which makes it easier to test and improve.
Focused agents outperform general ones
The same principle applies to the agents themselves. Rather than one agent that knows everything and can do anything, build several focused agents, each with a short system prompt, a small set of well-designed tools and the model that fits its task, coordinated by a lightweight orchestrator. Focused agents:
- use fewer tokens, since their instructions and tools are short;
- make fewer mistakes, since they have fewer ways to go wrong;
- are safer, since each one holds only the permissions its job requires;
- are easier to evaluate and to move to a better or cheaper model later.
Make the model a setting
Models change every few months, and so do their prices. Do not hard-code one. Make the model for each step a configuration value, keep a set of real test tasks for every agent, and when a new model comes out, run the tests and switch if it is better or cheaper. Caching repeated prompts and answers, and capping how many steps an agent may take, keep costs predictable as well.
A quick efficiency check for your agents
- Does any call send context the model does not need?
- Is retrieval filtered by the user’s permissions?
- Is your largest model doing work a small one could handle?
- Could one large agent be split into focused ones?
- Can you change the model for a step without a release, and do you have tests that tell you whether you should?
FAQ
How does a vector database reduce token usage?
It returns only the few entries closest in meaning to the question, so the prompt carries a few hundred relevant tokens rather than tens of thousands of background ones. Cost and latency fall accordingly.
Do I need a dedicated vector database?
Often not. The open-source pgvector extension adds vector search to PostgreSQL, alongside your existing data, filters and backups. Dedicated vector databases pay off at very large scale.
Why use several models in one AI agent?
Most steps, like classification, extraction or format checks, do not need the largest model. Small models handle them faster and cheaper, mid-sized models cover everyday reasoning, and the largest models are kept for planning and difficult decisions.
What are focused agents?
Agents with a short system prompt, a small set of tools and the model that fits their task, coordinated by a lightweight orchestrator. They use fewer tokens, make fewer mistakes and need fewer permissions than one general agent.
Smaller context and the right model for each step usually make agents cheaper, faster and more accurate at once. How do you keep your token bill under control? We would like to hear what works for you.
