I built an AI assistant inside Odoo 16 as a self-initiated project: people ask it policy and how-to questions in chat, and it answers from documents an admin uploads. Getting it to answer was the easy part; automating everything is easy to demo. The real work started when I asked four plain questions: is this worth automating, which model is good enough, what does one request cost, and how does it stay accurate when people type messy things?
How an LLM call is priced
A model reads and writes tokens: pieces of words, roughly three quarters of an English word each. You pay for the tokens you send (input) and the tokens it writes back (output), at separate rates per million, and output usually costs several times more than input. Prompt caching lets a provider keep a long, unchanging start of the prompt, so the next request re-reads it at a fraction of the input price. Writing it into the cache costs a little more than plain input, so caching pays only when the same prefix comes back often.
Is this worth automating at all?
Not every message needs a model. A bare "hi" or "thanks" gets a friendly line and never touches the documents. A question outside the assistant's scope gets one fixed reply, a few tokens long. The model is kept for questions whose answer is in the documents and can be checked against them. If a person can't check the output, or nobody asks often enough, the automation isn't worth its bill.
Which model is good enough?
The provider and the model are settings, not code, so a cheaper model can be tried without a deployment. The right prompt shape turned out to depend on the provider. Where caching is cheap, the whole knowledge base travels once and is cached. Where a free tier caps tokens per minute, an oversized prompt is simply refused, so only the few most relevant passages are retrieved. Some models also charge more per token above a certain prompt size, so I keep the prompt below that line on purpose. The biggest model is rarely the right default; the cheapest one that answers correctly is.
What does one request actually cost?
The API doesn't return a price. It returns token counts: plain input, tokens written to the cache, tokens read from it, and output. So the cost is mine to work out, one call at a time:
def estimate_cost(rate, usage):
per_token = lambda price: price / 1_000_000
return (usage.input * per_token(rate.input)
+ usage.cache_write * per_token(rate.cache_write)
+ usage.cache_read * per_token(rate.cache_read)
+ usage.output * per_token(rate.output))
Rates live in a table an admin edits, each row dated from when it applies. A row that has been in effect can't be changed, and every logged call is priced with the rate of its own day, so a price change never rewrites what last month cost. Keeping the four token counts apart matters too: when cache reads drop to zero, caching has quietly stopped working, and one combined number would hide it.
Build the limit before the prompt
Early on, the assistant got into a loop with another bot, each answering the other until I stopped it. A model writing "I'll stop now" changes nothing; the application has to enforce the boundary. Before any call is made, one check reads the call log:
- a switch that turns answering off;
- a per-person cap over a few minutes, which catches a loop within seconds;
- a per-person daily cap;
- daily and monthly caps for the whole company, on calls and on cost;
- a stop after repeated failures in the same chat.
block = gate.check(person, now)
if block:
notify(person, block.retry_after) # one short line, no model call
return
answer = call_model(question)
A blocked message costs nothing: no model call, just a short note saying when answering resumes. Every limit is a setting, and zero switches it off. Softer thresholds email the developers before anything is blocked, and each kind of alert is sent once a day (once a month for monthly ones), so a busy afternoon never floods an inbox.
Keeping it accurate when the input is messy
Accuracy came from scope more than from model size. Answers are grounded only in the uploaded documents. The person's own context is added, but sensitive fields (bank details, ID numbers, salaries, anyone else's data) never enter the prompt, because the prompt goes to an outside provider and is logged. The greeting check matches only a message that is nothing but a greeting: a false match would answer a real question with no documents at all. Replies have a length cap, and the prompt asks for fewer words than the cap allows, so answers aren't cut mid-sentence. Every call is logged with its tokens, timing and status, so any answer and its cost can be explained later.
AI earns its place where it is cheap enough to keep running and accurate enough to trust, and both are decided before the first prompt is written.
The pattern behind it: Enforce the rule where it can't be walked around