Skip to content
mentoor

Cut an LLM feature's cost by 70%

For engineers

Scenario lesson and practice set combining Caching strategies for LLM apps, Prompt caching, Batch and async jobs and Choosing a model for a task. Completing it counts as evidence for every listed skill.

The one-sentence idea: when a team wants AI to work on confidential documents, decide first what the documents are and who allows what, then choose a setup where the text stays inside the firm, and enforce access in the database, not in the prompt.

The case

Hartley & Voss is a 14-lawyer firm. Its lawyers lose hours searching old leases, NDAs and call notes. An engineer is asked to build an assistant that finds the right clause and summarizes it. Last week an associate pasted a client's lease into a personal chatbot account to get a quick summary. It worked well, and that is the problem: the quickest route is also the one the firm cannot see, audit or delete from.

The firm's engagement letters promise confidentiality. One matter is under a court seal. An NDA with a counterparty says confidential information must stay on systems the firm controls. These facts, not the technology, set the design.

Why it matters

Four skills meet in this one project. If any of them is missing, the others do not rescue the result.

  • Workplace confidentiality tells you what the material is and whether a tool may touch it.
  • Confidentiality and privilege covers the tool tier, settings, consent and what to do after a slip.
  • Running models locally lets the text stay on a machine the firm owns.
  • Vector search makes the documents findable, and it holds the permission boundary between matters.

How to do it

  1. Sort the documents. List folders and ask what each holds, which contract or seal applies and who owns it. Client matters, NDA material and sealed files go on the strictest list. The firm's blank templates can go anywhere the firm allows.
  2. Pick the tool tier. A free or personal chatbot account has no firm contract, no admin controls and may train on chats. A business plan has a signed agreement and settings an admin can lock. A model running on the firm's own server sends nothing to a third party. For sealed and NDA matters, use the third.
  3. Run the model locally. Install a runner such as Ollama or llama.cpp, pull an open-weight model and set a context size big enough for the documents. Keep the API on 127.0.0.1 and put an authenticated proxy in front of the chat UI. Read the load log: check GPU offload, context length and that the bind address is not 0.0.0.0.
  4. Index with pgvector. Store chunks and their vectors in the firm's existing Postgres, with a matter_id column. Use the distance operator that matches the embedding model, for example <=> with vector_cosine_ops. Take the allowed matter ids from the signed-in user's record on the server. Row-level security is a second lock.
  5. Handle filters honestly. A filter on an approximate index can return fewer rows than asked for. Test a user who sits on one small matter, and use iterative scans, a larger ef_search, partial indexes or partitions.
  6. Control logs and consent. Prompts and answers are client records and may be ordered produced in a dispute. Set a retention period, limit who reads the logs and redact where you can. Where text must go to a hosted provider, get informed client consent that names the data and the provider.
  7. Plan for slips. Tell staff to report a wrong paste the same day. The partner decides whether the client must be told, so speed matters more than tidiness.

Before and after

Before: associates paste files into personal accounts, one shared vector table serves every user, the model API listens on every interface, and the prompt says "only use matter A".

After: sealed and NDA matters use the local model, its API listens on localhost behind sign-in, the search query filters by the matter ids the server looks up, logs have a retention rule, and staff know to report a slip at once.

What goes wrong

  • A prompt used as a lock. The retrieval step has already put other matters' text in the model's context. Enforce access before retrieval.
  • A local model that is open to the network. "Local" means where it runs, not who can call it. Bind it to localhost and authenticate in front of it.
  • Silent truncation. A small context window drops the end of a long contract and the answer still sounds complete. Check the load log and compare answers with the source clause.
  • Redaction that leaves rare facts. A unique detail can identify a client when names are gone.
  • Hidden records. Chat logs and vectors are client data. They need the same retention and access care as the files.

Steps

  1. Break the bill down per route

    Skill: llmops-caching

    Log model, input tokens, cached tokens, output tokens and tenant on every call, then total cost per route. Here that shows chat at $10,800 of $13,950, so chat gets the most attention.

    Cost per route, last month
    RouteCallsCost
    Help-centre chat400,000$10,800
    Nightly ticket tagging300,000$1,800
    Weekly document summaries100,000$1,350
  2. Sort workloads into live and offline

    Skill: api-batch

    Ask who waits and how long the deadline is. Chat and agent suggestions are live. Tagging and summaries have a morning or Monday deadline, so they can use a batch API at about half price. Use unique custom ids and map results by id.

  3. Run a bake-off per route

    Skill: models-choose

    Replay 200 real past inputs per route on two or three models with one rubric. Set a bar for quality and latency first, then choose the cheapest model that clears it. Score by category so a weak slice such as refunds is visible, and check data location before sending personal data to any host.

  4. Put stable text first in every request

    Skill: context-caching

    Order the request as tools, rules, policy excerpts, then per-request content. Remove timestamps and names from the top, and sort any tool list. Then read cached tokens per call: a healthy chat route reads most of its prefix.

  5. Add a safe response cache

    Skill: llmops-caching

    Cache only routes whose answers do not depend on live data. Build the key from tenant, model, prompt version, knowledge-base version and the normalised question, and give each route its own time to live. Leave order status out.

  6. Move offline routes to batch

    Skill: api-batch

    Submit tagging nightly and summaries weekly. Poll, collect results by custom id, and resubmit only the failed or expired requests. Submit early enough that a slow batch still beats the deadline.

  7. Watch the savings every week

    Skill: llmops-caching

    Track cached share of input tokens, response-cache hit rate, batch failures and quality pass rate per route. A drop in the cached share is the first sign a deploy broke the prefix, long before the invoice shows it.

Skills this scenario uses

Where to study