Short answer: start with a well-written prompt and a strong hosted model. Add retrieval (RAG) when the model needs your data. Consider fine-tuning last, and only for behaviour, not knowledge. Before any of that, collect real examples with known good results, because without an evaluation set you cannot tell whether any change is an improvement. Then engineer the surroundings: privacy, guardrails, streaming, fallbacks and human review.
Start with the task, not the model
Write down four things before choosing any technology:
- Input and output. What goes in, what comes out, and in what format. “Support email in; category, urgency and a draft reply out.”
- Who consumes the output. A person who can spot errors, or code that will act on it without review.
- The cost of a wrong answer. Mild annoyance, a lost customer, or a legal problem.
- Twenty real examples with the result you would consider correct.
If you cannot produce the examples, the task is not yet defined well enough to build. If a fixed rule would produce the right output, write the rule. The broader question of whether AI suits the problem is covered in how to build an AI product.
Prompting, RAG or fine-tuning
| Prompting alone | Retrieval (RAG) | Fine-tuning | |
|---|---|---|---|
| What it is | Instructions, examples and the user’s input in the prompt | Search your data at request time; put the relevant passages in the prompt | Further training of a model on your examples |
| Best for | Tasks where everything needed is in the input: summarising, rewriting, classifying, extracting | Answers that depend on your documents, records or product data | Consistent style or format; narrow tasks at high volume; letting a smaller model match a larger one |
| Adds new knowledge | Only what fits in the prompt | Yes, and it stays current as the data changes | Unreliably; a poor way to teach facts |
| Can cite sources | No | Yes | No |
| Respects per-user access rules | Not applicable | Yes, if retrieval filters by permission | No; whatever was trained in is available to everyone |
| Set-up effort | Hours to days | Weeks | Weeks, plus a labelled dataset |
| Upkeep | Re-test when the model version changes | Keep the index in sync with the source data | Re-train and re-evaluate when the base model is replaced |
| Typical failure | The model lacks context and guesses | Retrieval returns the wrong passages, so the answer is confidently wrong | Overfits to the examples; quality declines elsewhere |
Decision rules
- Try prompting first, with a capable model, clear instructions and a few examples. Measure it.
- If failures are caused by missing information, add retrieval.
- If the information is small and stable, such as a policy document of a few pages, skip retrieval and place it in the prompt, using the provider’s prompt caching to keep cost down.
- If failures are about consistent format or tone despite good prompts, or you need a small, cheap model to perform a narrow task at volume, consider fine-tuning.
- These combine. A fine-tuned model can still use retrieval, and most agents use all of prompting, retrieval and tools.
Getting retrieval right
In a RAG system, answer quality is capped by retrieval quality. If the right passage is not in the prompt, no model will rescue the answer. The parts that matter:
- Parsing. Clean text out of PDFs, slides, spreadsheets and web pages, keeping headings and tables. Scanned documents need OCR. This step is dull and decides a great deal.
- Chunking. Split along the document’s own structure (sections, headings, question-and-answer pairs), not at arbitrary character counts. Store the title and section path with each chunk.
- Hybrid search. Combine vector similarity with keyword search. Embeddings are good at meaning and weak at exact product codes, names and numbers; keyword search is the reverse.
- Reranking. Retrieve generously, then reorder with a reranking model and keep the top few. Fewer, better passages improve both quality and cost.
- Permissions. Filter by the asking user’s access rights inside the search query, before anything reaches the model. Filtering afterwards risks leaks through summaries.
- Freshness. Re-index when sources change, and remove deleted content from the index promptly.
- Citations and refusal. Have the model cite the passages it used, and tell it to say when the sources do not contain the answer.
Evaluate retrieval on its own: for each test question, was the passage containing the answer among the top results? That single number locates most RAG problems faster than reading generated answers.
Data privacy and security
An LLM call sends data to a third party. Treat it like any other sub-processor, with some points particular to the technology.
- Read the provider’s business API terms on three points: whether inputs are used for training, how long they are retained, and where they are processed. Consumer chat products often have different terms from the API. Terms change, so record the date you checked.
- Sign a data processing agreement and add the provider to your own sub-processor list where your customer contracts require it.
- Send the minimum. Remove fields the task does not need; consider masking names, emails and identifiers before the call and restoring them afterwards.
- Check regional processing options if you have residency commitments.
- Keep tenants apart in multi-tenant products: retrieval scoped by tenant, no shared caches across tenants, no customer data in prompts shared between customers.
- Decide what you log. Prompts and outputs are valuable for debugging and may contain personal data. Set retention and access rules for those logs.
- Keep provider API keys on the server, never in a browser or mobile app.
Prompt injection
Any text the model reads can contain instructions: a web page, an uploaded PDF, an incoming email. A model cannot reliably tell your instructions from an attacker’s. Design on the assumption that injection will sometimes succeed:
- Treat all model output as untrusted input to the rest of your system
- Give tools the narrowest permissions possible, acting as the current user, never as an administrator
- Require human confirmation for actions that send data out, spend money or delete things
- Do not place secrets in prompts
- Be especially careful when one feature combines untrusted content, access to private data and an ability to communicate externally; removing any one of the three greatly reduces the risk
Not legal advice. Data-protection duties depend on your jurisdiction, your customers and the kind of data. Involve whoever is responsible for privacy in your company before sending personal data to any new provider.
Evaluation: the part teams skip
Without evaluation, prompt work is guesswork: a change that fixes one example quietly breaks three others. A workable process for a small team:
- Collect 50–200 real inputs covering typical cases, edge cases and inputs that should be refused. Add every production failure you find later.
- Define “correct” for each. An exact label, facts that must appear, facts that must not, or a reference answer.
- Automate the scoring. Code checks where possible: valid schema, correct category, required citation present, number matches. Use a model as a grader for open-ended text, with a written rubric, and spot-check the grader against human judgement.
- Keep a baseline score and run the set on every change to prompts, models, retrieval settings or tools.
- Track cost and latency in the same run. A change that gains two points of quality and doubles the cost is a business decision.
- Continue in production. Thumbs up/down, edits users make to drafts, escalation rates, and a weekly review of a sample of real conversations.
Agree the pass mark with the business before building. “92% of categories correct and no wrong refund amounts” is a target. “It should be accurate” is not.
Guardrails
| Layer | Measures |
|---|---|
| Input | Length limits, rate limits per user and tenant, file type and size checks, abuse and topic screening for public-facing features |
| Prompt | Clear scope (“answer only questions about X from the provided sources”), explicit refusal behaviour, a clear boundary between instructions and untrusted content |
| Output | Structured output validated against a schema; business-rule checks in code (does this ID exist, is this amount within limits); citation required; moderation check where content reaches the public |
| Actions | Allow-list of tools, scoped credentials, dry-run mode, human confirmation for irreversible steps, limits on steps and spend per task |
| Operations | Logging, spend alerts, anomaly alerts, a switch that turns the feature off without a deployment |
The guiding idea: the model proposes and your code decides. Anything that must always be true belongs in code, not in a prompt.
Latency and streaming
Responses from language models take seconds, not milliseconds, and output is generated token by token. What users perceive is mostly the time until something appears.
- Stream conversational and long-form output to the interface as it is generated.
- Do not stream into code. When the output is structured data for your system, wait for the full response, validate it, then act.
- Move long work to background jobs with progress updates. Agents and document processing should not hold a web request open.
- Cut tokens. Shorter prompts and capped output lengths reduce latency as well as cost.
- Use a smaller model for steps that do not need a large one, such as classification and routing.
- Run independent calls in parallel, and cache stable prompt prefixes where the provider supports it.
- Show progress honestly in the interface: “searching documents”, “drafting”. A named step feels shorter than a spinner.
Fallbacks and failure handling
Provider APIs have outages, rate limits and slow periods. Model versions are retired on a schedule. Plan for each.
- Timeouts and retries with backoff for transient errors, with a ceiling so that retries do not multiply cost.
- A provider abstraction. One internal interface for “complete this prompt”, so that a second model or provider can be configured as a fallback. Test the fallback against your evaluation set, because prompts do not transfer perfectly between models.
- Graceful degradation. If the AI step fails, the product should still work: show the search results without the generated summary, queue the task, or pass it to a person.
- Invalid output. When schema validation fails, retry once with the error included, then fall back. Never pass unvalidated output downstream.
- Pinned model versions. Use explicit version identifiers, not “latest” aliases, so that behaviour changes only when you decide. Put provider deprecation dates in your calendar.
- Budget circuit-breakers. Per-tenant and global spending limits that degrade the feature before they surprise finance.
Human in the loop
The right level of autonomy depends on how reversible the action is and how well the error rate has been measured.
| Pattern | How it works | Use when |
|---|---|---|
| Suggest | AI drafts; a person edits and sends | Customer-facing communication, anything carrying legal or financial weight, new features with unmeasured error rates |
| Approve | AI prepares a complete action; a person clicks approve or reject | Record updates, refunds within policy, routing decisions |
| Act, then review | AI acts; a sample is audited; low-confidence cases are escalated | High-volume, low-stakes, reversible actions with a measured and acceptable error rate |
| Fully automatic | No review | Trivial, reversible tasks such as tagging or internal summaries |
Build the review interface with care: show the sources, make editing quick, and record every correction. Those corrections are your best evaluation data, and the measured approval rate is the evidence you need before moving a task one row down the table.
A sensible build order
- Task definition and twenty examples
- Prompt-only prototype on a strong model; measure
- Expand the evaluation set; add retrieval if failures are about missing knowledge
- Structured outputs and validation
- Privacy review: provider terms, data minimisation, logging policy
- Interface with streaming, error states and feedback capture
- Guardrails, limits, metering and spend alerts
- Fallbacks and the off switch
- Try smaller models against the evaluation set to reduce cost
- Limited rollout, weekly review of real usage, then wider release
Steps 1 to 3 form the prototype stage and answer whether the feature is worth building. What each stage costs, including the token arithmetic, is in AI integration cost. If the feature lives in a multi-tenant product, the tenancy and metering groundwork is described in our SaaS architecture guide. And if you would like a team to carry this out, BBR’s AI development and LLM integration service follows this sequence.
