Foundations to Frontier
Tests

LangChain and LlamaIndex

LangChain and LlamaIndex are the most widely used frameworks for building LLM applications. They provide prompt templates, model wrappers, document loaders, text splitters, retrievers, vector-store integrations, chains and agents, plus tracing and evaluation tools. LangChain is built around composable 'runnables' and LangGraph for stateful agent graphs. LlamaIndex centres on indexing and querying data: nodes, indexes, retrievers, query engines and workflows. This page covers what each provides, how the abstractions map to the plain code underneath, common patterns such as RAG, routing and summarisation chains, and when a framework helps or gets in the way. In the exercises you write a pipe, a prompt template and a recursive text splitter. You then simulate four ways to summarise a 64-chunk document, scoring how many of 24 important facts reach the final summary. Under these assumptions: stuffing what fits in a 16-chunk window kept 0.19, map-reduce 0.42 in 21 calls over 3 sequential steps, and refine 0.26 over 16 sequential steps. Refine forgot early content: it kept 0.12 of the first half against 0.40 of the second. One call to a long-context model kept 0.46.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Kits for building LLM applications

Frameworks such as LangChain and LlamaIndex are like construction kits: ready-made pieces for loading documents, splitting them, searching them, filling in prompts and calling models, which snap together into a pipeline. They save time, but every piece hides choices that matter, such as how text is cut and how long documents are summarised. The demo shows how chunk size changes retrieval.

Retrieval lab
Retrieval quality at 256 tokens: 0.92 (relative). Too small loses context; too large dilutes the match.
Why engineers care

An engineering-document assistant can be prototyped in an afternoon with these kits; production versions often replace parts with plain code once the design is clear.

📝 Quick check (Level 0)
0/3 answered
  1. 1. These frameworks provide…
  2. 2. LlamaIndex centres on…
  3. 3. Framework pieces hide…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • LangChain: chat model wrappers for many providers, prompt templates, output parsers, the runnable interface (invoke, batch, stream; composed with |), retrievers, tools; LangGraph for stateful, cyclic agent workflows with checkpoints; LangSmith for tracing and evaluation.
  • LlamaIndex: readers (LlamaHub), node parsers, VectorStoreIndex and other indexes, retrievers, response synthesisers (compact, refine, tree summarise), query and chat engines, workflows, LlamaParse for documents.
  • Common patterns: RAG, routing between indexes or chains, map-reduce and refine summarisation, structured extraction, agents with tools, memory.
  • Alternatives: plain SDK code, DSPy (programs with optimised prompts), Haystack, Semantic Kernel, Pydantic AI.
  • Trade-offs: fast prototyping and integrations vs extra abstraction, version churn, hidden prompts and defaults; inspect the prompts actually sent.
📝 Quick check (Level 1)
0/3 answered
  1. 1. LangGraph is for…
  2. 2. LlamaIndex's tree summarise is a…
  3. 3. A framework alternative that optimises prompts:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Using the frameworks well

Exercise 1.

  • Composing steps into a pipeline.
  • A prompt template with checks.
  • A recursive text splitter.

Practice.

  • Prototype with the framework; turn on tracing (LangSmith, Phoenix, Langfuse) and read the actual prompts and calls.
  • Pin versions; frameworks change quickly.
  • Choose summarisation by document length and latency budget: one long-context call, map-reduce in parallel, or refine when order matters.
  • Keep business logic in plain functions; wrap them as tools or runnables.
  • Evaluate the pipeline end to end with a custom eval (F14-5).
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_messages([("system", "Summarise for a maintenance engineer."), ("human", "{text}")])
chain = prompt | ChatAnthropic(model="claude-sonnet-5-5") | StrOutputParser()
partials = chain.batch([dict(text=c) for c in chunks], config=dict(max_concurrency=8))   # map, in parallel
final = chain.invoke(dict(text="\n\n".join(partials)))                               # reduce
🐍 Exercise 1: pipelines, templates and splitting
Compose steps, fill a prompt template safely and split text recursively.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Read the actual prompts with…
  2. 2. Framework versions should be…
  3. 3. Map steps should run…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Summarisation strategies. Stuffing sends everything that fits in one call; anything beyond the window is lost. Map-reduce summarises chunks independently (parallel) and then summarises the summaries; depth is log_g(n) levels for fan-in g. Refine walks the document sequentially, updating a running summary; it sees order and context but takes n/g sequential calls, and each rewrite can drop earlier facts.

Information loss. If each call keeps a fact with probability p, a fact passing through d levels survives with about p^d; refine’s early facts pass through many rewrites, so p^(number of later steps) decays. A length cap on summaries forces further selection, where importance judgements are noisy.

Latency. With parallel map calls, map-reduce latency scales with depth, not with the number of chunks.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Map-reduce depth for 64 chunks with fan-in 4:
  2. 2. Refine's early facts survive about…
  3. 3. Map-reduce latency scales with…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Which summarisation chain keeps the most important information from a long document, and at what cost in calls and latency?

History.

  • LangChain (H. Chase, 2022) and LlamaIndex (J. Liu, 2022).
  • J. Wu et al., “Recursively summarizing books with human feedback” (2021).
  • N. Liu et al., “Lost in the middle” (2023).

Exercise 2. A simulated summariser.

  • Document: 64 chunks with 3 facts each; 24 facts are important.
  • One call: each fact survives with probability 0.97·exp(−n/400) for n input facts, lower in the middle of long inputs; at most 30 facts survive, chosen by importance plus noise.
  • Strategies: stuff the first 16 chunks; stuff everything into a long-context model; map-reduce with fan-in 4; refine in groups of 4.
  • Trials: 200 documents.
strategy important facts kept first half second half LLM calls sequential steps
stuff (16-chunk window) 0.191 0.383 0.0 1 1
stuff (long context) 0.46 0.465 0.454 1 1
map-reduce 0.422 0.424 0.417 21 3
refine 0.257 0.115 0.395 16 16
  • Truncation lost the whole second half.
  • Map-reduce covered the document evenly in 3 sequential steps.
  • Refine was slowest and forgot early content.
  • One long-context call did slightly better than map-reduce under these assumptions; with a stronger ‘lost in the middle’ effect, map-reduce would win.
  • Seeds 1 and 2: map-reduce 0.413 and 0.416; refine 0.234 and 0.229; long context 0.449 and 0.456.
  • Caveat: all retention parameters are assumptions, not measurements of a real model; measure on your documents.
🐍 Exercise 2 (research): which summarisation chain keeps the most important information from a long document, and at what cost in calls and latency?
Simulate stuffing, long-context stuffing, map-reduce and refine summarisation of a long document; measure recall of important facts, calls and sequential steps.

LLM frameworks in AI, robotics and aerospace

  • Document assistants: loaders and parsers for PDFs and drawings, indexes over manuals, and summarisation of long reports.
  • Agents: LangGraph-style state machines for multi-step engineering workflows with human approval nodes.
  • Production: many teams keep frameworks for prototyping and tracing, then simplify to plain code for audited deployments.

References

  1. H. Chase (2022). LangChain, open-source framework and documentation.
  2. J. Liu (2022). LlamaIndex, open-source framework and documentation.
  3. J. Wu et al. (2021). “Recursively summarizing books with human feedback.” arXiv.
  4. N. F. Liu et al. (2024). “Lost in the middle: how language models use long contexts.” TACL.