Learn LLM

Chapter 15: Using and Evaluating LLMs

Prompt engineering, RAG, benchmarks, and failure modes.

Prompt engineering

The way you ask a model changes the answer you get. Zero-shot prompting gives the model an instruction with no examples. Few-shot prompting includes a few demonstrations of the desired input-output format. Chain-of-thought prompting asks the model to explain its reasoning before giving the final answer.

Zero-shot: "Classify the sentiment as positive or negative: 'I love this movie.'"
Few-shot: "Input: good -> positive. Input: bad -> negative. Input: 'I love this movie.' ->"
Chain-of-thought: "Think step by step, then answer: what is 17 + 24?"

Clear instructions, specific formats, and role descriptions help. For example, telling the model to act as a careful programming assistant and to return JSON can produce more reliable output. Prompts can also be system prompts, user prompts, or assistant prompts depending on the chat format.

Retrieval-augmented generation

LLMs have a fixed knowledge cutoff and can hallucinate facts. Retrieval-augmented generation, or RAG, grounds the model in an external knowledge base. The system embeds a query, retrieves relevant documents, and includes them in the prompt before generating an answer.

  • Index your documents into a vector database.
  • Convert the user query into an embedding.
  • Retrieve the top-k most similar chunks.
  • Inject the retrieved chunks into the prompt.
  • Generate the answer conditioned on those chunks.
def answer(query, docs):
    query_emb = embed(query)
    top_chunks = vector_db.search(query_emb, k=3)
    context = "

".join(top_chunks)
    prompt = f"Answer based on context:
{context}

Question: {query}"
    return generate(prompt)
RAG retriever

Type a query and see which documents are retrieved and how they are inserted into the final prompt.

RAG retriever
Top retrieved chunks
#1 (score 2.00)

Transformers stack many identical blocks each with attention and feed-forward layers.

#2 (score 1.00)

Attention lets every token look at every other token in parallel.

Augmented prompt
Context:
• Transformers stack many identical blocks each with attention and feed-forward layers.

• Attention lets every token look at every other token in parallel.

Question: how do transformers use attention
Answer:

Tool and function calling

Models can be taught to emit structured calls to external tools. A special syntax lets the model request a calculator, search engine, database query, or API. The calling code executes the tool and returns the result, which the model then uses to compose the final response.

{
  "tool": "get_weather",
  "parameters": { "location": "Paris", "unit": "celsius" }
}

This turns an LLM into a reasoning layer that orchestrates external capabilities. It is especially useful when the model needs real-time data, precise calculations, or private information that was not in its training set.

Benchmarks and evaluation

Academic benchmarks test broad capabilities. MMLU measures knowledge across many subjects. HellaSwag tests commonsense reasoning. HumanEval tests code generation. GSM8K tests grade-school math. TruthfulQA tests whether models reproduce false beliefs.

Benchmarks are useful but imperfect. Models can be trained on benchmark data, making scores less meaningful. Real-world evaluation should also include human review, user satisfaction, task-specific metrics, and safety checks.

Failure modes

LLMs can hallucinate facts, generate biased outputs, leak training data, or be jailbroken into producing harmful content. They can also be confidently wrong, which is dangerous when users trust them as authorities.

  • Hallucination: inventing facts, citations, or code.
  • Bias: reproducing stereotypes from training data.
  • Overconfidence: stating incorrect information with certainty.
  • Jailbreaking: bypassing safety instructions with carefully crafted prompts.

Safety and red teaming

Red teaming involves adversarial testing to find harmful outputs before deployment. Safety filters can be at the input level, the output level, or both. Content moderation classifiers, refusal training, and system prompts are common defenses, but no system is perfect.

Building safe LLM applications is not only a technical problem but also a policy and user-experience problem. Clear expectations, feedback loops, and human oversight are essential.

Deployment and quantization

Deploying an LLM requires choosing inference hardware, batching strategy, and serving framework. Quantization reduces model weight precision, for example from 16-bit to 8-bit or 4-bit, which cuts memory and speeds up inference with modest accuracy loss.

Techniques like GPTQ, AWQ, and GGUF make 4-bit inference practical on consumer GPUs and even CPUs. Quantization-aware training and methods such as QLoRA allow fine-tuning in very low precision. The right trade-off depends on latency, memory, and quality requirements.

Key takeaway

Using an LLM well means more than just calling generate. Prompt engineering, retrieval augmentation, tool use, careful evaluation, and safety guardrails turn a raw model into a reliable product. Quantization and serving optimizations make deployment feasible.

End of the journey

We started with n-grams and sparse vectors, moved through neural networks and sequence models, reached the transformer, and finally built, trained, fine-tuned, and deployed modern language models. The field moves fast, but these fundamentals will stay relevant.

Read more