AI Engineering: LLMs, RAG & Agents
AI Engineering: Complete Guide to LLMs, RAG and Agents
AI engineering is the discipline of turning models into reliable software systems that solve defined problems. For applications built with large language models, that means more than writing prompts: engineers connect data, retrieve evidence, manage tools, evaluate outputs and operate services safely. A complete application needs a deliberate architecture around the model, with explicit controls for access, cost, latency and failure. This guide follows a worked example: an internal support assistant that answers questions from company documentation and can prepare service tickets. The same lifecycle applies to many knowledge assistants, customer support tools and document-heavy workflows, although their risk profiles differ.
The assistant’s first release will answer questions with citations; a later release will propose actions through controlled tools. Building it requires decisions about document processing, dense retrieval, vector storage, retrieval augmented generation, fine-tuning and agent orchestration. Each decision affects the others: chunking changes retrieval quality, permissions constrain search, and tool access raises the standard for evaluation. The sections below explain how those components fit together and how to test and deploy the resulting system. For structured study alongside this technical guide, Erudex offers [AI Engineering: LLMs, RAG & Agents](/courses/ai-engineering). Careers, learning costs and exam preparation belong in the companion article.
Key points
- •Design AI engineering around explicit tasks, permissions and failure behaviour.
- •Validate retrieval quality before changing prompts or fine-tuning models.
- •Keep tool authority and approval controls outside the model.
- •Version, evaluate and monitor the complete system, not just the LLM.
Define the task and map the LLM system architecture
Start by specifying what the assistant may answer, which sources count as authoritative and what it must never do. In this example, employees ask questions about approved internal procedures. The assistant should retrieve relevant passages, produce a grounded answer and cite the source documents. If evidence is missing or contradictory, it should explain the limitation rather than invent a policy. Ticket creation is a separate capability requiring explicit confirmation. Define success through representative tasks and measurable constraints, such as answer correctness, citation support, response time and cost per successful request. These requirements should drive model selection, not the other way round.
The LLM system architecture has two connected paths. An ingestion pipeline reads documents, extracts text, attaches metadata, creates chunks, computes embeddings and updates a searchable index. A request pipeline authenticates the user, interprets the question, retrieves authorised evidence, constructs a prompt and calls the model. It then checks the output and returns an answer with source references. A controlled tool layer handles any approved actions. Around both paths sit evaluation, tracing, configuration management and operational monitoring. Keep these boundaries explicit so that replacing an embedding model, changing a prompt or switching inference providers does not require rebuilding the entire application.
Prepare documents and implement dense retrieval
Retrieval quality begins with document quality. Extract useful text while preserving headings, tables, document identifiers and meaningful relationships between sections. Remove navigation noise and duplicated material, but retain details that change an instruction’s meaning. Chunk documents along semantic boundaries where possible, then test chunk sizes against actual questions. Small chunks can omit necessary context; large chunks can bury the answer and increase prompt costs. Attach metadata such as ownership, source location, revision date and access policy before indexing. For the support assistant, a procedure should remain traceable to its current approved version, rather than appearing as an anonymous block of text.
Dense retrieval represents queries and document chunks as numerical vectors produced by an embedding model. A similarity search finds passages whose representations are close to the query, enabling matches even when wording differs. Document and query embeddings must use compatible models and any required input formats. Dense search can still miss exact product codes, unusual names or negation. Hybrid retrieval combines it with lexical search, often followed by a reranker that scores candidate passages against the question. Retrieve enough candidates to preserve recall, then select a smaller evidence set. Validate this pipeline using labelled queries rather than assuming semantic similarity guarantees relevance.
Choose vector storage and keep the index trustworthy
A vector database stores embeddings and supports similarity search, usually alongside metadata filtering and index management. Some teams use a dedicated service; others add vector capabilities to an existing database or search platform. Choose according to filtering requirements, update frequency, operational experience and observed workload. Approximate nearest-neighbour indexes trade some retrieval accuracy for faster search, so configuration deserves testing with realistic data. Distance functions, vector dimensions and embedding normalisation must match the embedding model’s requirements. The database is not the source of truth: keep original documents and provenance separately, with stable identifiers connecting each retrieved chunk to its authoritative source.
Index maintenance is part of application correctness. When a policy changes, the ingestion pipeline should replace or retire affected chunks without leaving stale versions silently discoverable. Use repeatable processing, explicit document versions and deletion propagation. A change of embedding model generally requires re-embedding the corpus and rebuilding or migrating the relevant index; mixing incompatible embeddings undermines search. Access filtering must also remain correct during updates. Enforce permissions before unauthorised content can reach the model, and verify that the chosen search configuration handles selective filters reliably. Test incremental updates, full rebuilds and recovery procedures before the assistant becomes an operational dependency.
Decide between RAG and fine-tuning
Retrieval augmented generation, or RAG, supplies retrieved evidence to a model at request time. For the support assistant, the prompt should distinguish system instructions, the user’s question and quoted source material. Ask the model to ground policy claims in that evidence and associate citations with specific claims. Keep source identifiers attached throughout prompt construction and rendering. RAG makes changing knowledge easier to maintain because documents can be updated without retraining the generator. However, it does not guarantee factual answers: retrieval may fail, sources may conflict, and the model may misinterpret evidence. Citation presence alone is not proof that a claim is supported.
The practical RAG vs fine-tuning decision is about the problem being solved. Use retrieval when answers depend on current, private or attributable knowledge. Consider fine-tuning when a model repeatedly struggles with a stable behaviour, specialised output pattern or task despite good prompting and sufficient context. Fine-tuning adjusts model weights using training examples; it is not a dependable replacement for an updatable factual store. The approaches can be combined, with a tuned model answering from retrieved evidence. Before training, establish a baseline, inspect failures and protect a separate evaluation set. Otherwise, apparent improvement may reflect memorisation or changes elsewhere in the pipeline.
Add AI agents only where controlled orchestration helps
AI agents use model outputs to choose actions, call tools or adapt a sequence of steps towards a goal. This flexibility is useful when a request cannot be handled by one retrieval-and-answer pass, but it adds uncertainty and operational complexity. For the support assistant, a deterministic workflow may be enough: retrieve the procedure, collect required ticket fields, show a preview and request approval. Use agentic planning only where its flexibility produces a measurable benefit. A bounded workflow is usually easier to test than an open-ended loop that can repeatedly search, revise its plan and invoke external services without clear stopping conditions.
Treat every tool as a typed interface with a documented purpose, validated arguments and narrowly scoped credentials. The model may propose a ticket, but application code should verify permissions and require confirmation before submission. Enforce limits on tool calls, execution time and spending outside the model. Make write operations idempotent where possible so retries do not create duplicate tickets. Persist workflow state explicitly, including completed actions and outstanding approvals, rather than relying on conversation text alone. Tool responses are also untrusted inputs: a retrieved page or service error must not become an instruction that overrides the assistant’s rules or authorises further actions.
Evaluate retrieval, answers and actions separately
LLM evaluation should measure the complete application while also isolating failing components. Build a dataset of representative questions, expected evidence and acceptable outcomes, including ambiguous requests and cases where the assistant should abstain. For retrieval, measure whether relevant passages appear within the candidate set using metrics such as recall at a chosen cutoff. Where relevance labels support it, ranking metrics help assess ordering. For answers, examine correctness, evidence support, citation accuracy and completeness. For tools, test argument validity, permission enforcement and action success. Track latency and cost alongside quality, because an improvement that breaches operational constraints may not be usable.
Automated checks are useful for structured outputs, citation identifiers and known policy violations. Model-based judges can help assess open-ended answers, but they require clear rubrics and calibration against human review. Avoid treating a judge model’s preference as objective truth. Keep evaluation examples separate from prompt development and fine-tuning data, and maintain slices for difficult documents, languages, permissions and failure scenarios. Run regression tests whenever models, prompts, chunking or retrieval settings change. In production, sample traces responsibly and investigate negative feedback. User satisfaction alone cannot establish factual reliability, especially when an incorrect answer sounds confident or an unauthorised action appears convenient.
Secure the application across data and tool boundaries
The main security boundary is the application, not the prompt. Authenticate users, enforce document-level authorisation and apply least privilege to every service account. Prompt injection occurs when untrusted content tries to redirect model behaviour, including through retrieved documents or tool outputs. Clear instruction boundaries and model-side safeguards can help, but they do not provide complete protection. Prevent retrieved text from granting permissions, selecting arbitrary credentials or bypassing approval gates. Restrict network destinations and available tools according to the task. A support assistant should not acquire broad administrative access merely because a user asks it to troubleshoot a problem comprehensively and quickly.
Data protection also covers embeddings, prompts, conversation history, caches and telemetry. Embeddings should not be treated as anonymised data merely because they are numerical. Check provider policies and contractual controls for retention, training use, processing location and deletion against organisational requirements. Redact sensitive values where appropriate, encrypt data in transit and at rest, and avoid logging credentials or unnecessary personal information. Test cross-user and cross-tenant leakage, including cache collisions and stale access rules. For higher-impact actions, provide clear previews, approval records and audit trails. Plan how to revoke access, disable a tool and investigate a suspected disclosure without depending on model cooperation.
Deploy, scale and operate the application with LLMOps
Deploying LLM applications requires managing variable inference latency, token consumption and external service limits. Choose managed inference or self-hosting according to privacy needs, workload, model availability and operational capacity. Separate ingestion workers from interactive requests so a large document update cannot exhaust the answering service. Apply timeouts, bounded retries, concurrency limits and backpressure. Streaming can improve perceived responsiveness, but partial output may need buffering or checks before display in sensitive workflows. Cache only where freshness and authorisation rules permit it. For this assistant, cache keys must distinguish relevant permissions and document versions rather than sharing answers solely because questions look similar.
LLMOps extends deployment discipline to models, prompts, retrieval configuration, datasets and evaluation results. Version these artefacts together so each release is reproducible and reversible. Trace requests across retrieval, reranking, generation and tools, recording useful timings and identifiers without exposing sensitive content. Monitor failure rates, retrieval misses, token use and latency distributions, then compare them with the evaluation baseline. Release changes gradually, with rollback criteria and safe fallbacks when dependencies fail. If evidence retrieval is unavailable, the assistant should disclose that limitation rather than confidently improvise internal policy. Scale from measured bottlenecks: adding inference capacity will not fix slow filtering or a broken ingestion pipeline.
Frequently asked questions
- What is the difference between AI engineering and machine learning engineering?
- The disciplines overlap, and job titles vary between organisations. Machine learning engineering often centres on training, serving and maintaining predictive models and their data pipelines. AI engineering commonly includes integrating existing models into complete applications, especially systems involving prompts, retrieval, tools and user-facing workflows. Both require software engineering, evaluation and operational discipline. An LLM application may need substantial AI engineering without training a foundation model, while still relying on machine learning expertise for embeddings, reranking or fine-tuning.
- Do I need a vector database to build a RAG application?
- No. RAG requires a retrieval mechanism, not a particular database category. A small application might use in-memory vector search, while another retrieves evidence through keyword search, SQL queries or an existing search service. A vector database becomes useful when semantic search must support persistent storage, indexing, filtering and operational scale. Select it based on measured requirements. Dense retrieval is one option within RAG, and hybrid retrieval can be preferable when exact terms and semantic matches both matter.
- Is RAG better than fine-tuning for company documents?
- RAG is usually the better starting point when answers must reflect changing company documents and include attributable evidence. It allows source updates and access controls without retraining the generator. Fine-tuning is more appropriate for persistent behavioural problems, such as following a specialised response format or performing a repeatable task. It does not reliably turn model weights into an authoritative document repository. Test retrieval and prompting first, then consider combining RAG with fine-tuning if evaluation identifies a suitable remaining gap.
- How do you reduce hallucinations in LLM applications?
- Improve the evidence pipeline, constrain the task and evaluate unsupported claims explicitly. Retrieve relevant, current sources; preserve context; require claim-linked citations; and define when the assistant should abstain. Validate structured outputs and tool arguments in application code. Lower randomness can make outputs more consistent, but it does not guarantee truth. Human review remains appropriate for consequential decisions. Measure both incorrect answers and unnecessary refusals so that reducing hallucinations does not simply produce an assistant that avoids answering useful questions.
- When should an application use an AI agent instead of a workflow?
- Use a deterministic workflow when the steps and decision rules are known. Consider an agent when the application must adapt its sequence of actions to information discovered during execution, and that flexibility improves measured outcomes. Even then, limit the available tools, execution budget and permissions. Require approval for consequential actions and record state outside the model. The choice is not binary: a controlled workflow can contain one bounded agentic step without giving the model control of the entire process.
- How can I practise building a complete LLM application?
- Build a small documentation assistant with a fixed evaluation set, then add capabilities incrementally. Start with retrieval and citations, test permissions, introduce one controlled tool and practise deploying a versioned release with monitoring. Keep a failure log so changes address observed problems. The Erudex course linked above can support structured study, while [practice tests](/practice) can help check conceptual understanding. Treat a course certificate as evidence of course completion, not a substitute for demonstrating a secure, evaluated and working application.
Study it properly: AI Engineering: LLMs, RAG & Agents
Architect, evaluate, and deploy scalable LLM systems, dense retrieval pipelines, and autonomous agentic workflows.