Cookbook
Complete programs for the things you would actually build, each on one page: the problem, the program, how to run it, what to change for a real provider, and the pitfalls.
Every recipe is a small Scala program in
modules/samples,
and every one runs with no API key: by default a scripted client stands in for the model, so you can see the whole
flow work in seconds. Add --live to run the same code against the provider your configuration names
(llm4s.providers.provider, see running the samples).
To run the recipes with no local setup at all, open a GitHub Codespace from the
playground: it has sbt and a small local model, so --live works there too.
CI runs every recipe against its scripted client on every pull request, and checks that the program on each page is the source file, so what you read compiles and works.
A recipe solves one problem on one page. For one application built a part at a time, with exercises, follow the support-assistant tutorial.
The recipes
| Recipe | What it shows |
|---|---|
| Classify text into an enum | completeStructured reads a reply into a case class whose category is one of a fixed set |
| Extract structured data from an email | a nested schema with a list of line items, checked against the email |
| Summarise a long document | map-reduce: summarise each part, then the summaries |
| Answer questions over a folder of files | the RAG pipeline: ingest a folder, embed, retrieve, answer with sources |
| An agent that calls two tools | a tool of your own and the built-in calculator, chained by the model |
| A guardrailed chatbot | input and output guardrails around an agent |
| Stream tokens to the terminal | streamComplete: print the reply as it is generated |
| Fall back between providers | ReliableClient retries, then a second provider |
| Cache repeated calls | CachingLLMClient: answer a repeated question without a model call |
| Evaluate an answer with a judge | LLM-as-judge: grade an answer against a reference, 1 to 5 |
| Answer questions with keyword search | a BM25 keyword index, no embedding model |
| Remember facts between turns | a memory manager feeding the system prompt |
| Several agents in one graph | parallel specialist agents and an editor, joined in a graph |
Running a recipe
1
2
sbt "samples/runMain org.llm4s.samples.cookbook.StructuredOutputRecipe" # scripted client, no API key
sbt "samples/runMain org.llm4s.samples.cookbook.StructuredOutputRecipe --live" # your configured provider
For --live, the samples default to a local Ollama model (ollama pull llama3 first). To use another provider, add
a section to the git-ignored modules/samples/src/main/resources/application.local.conf and select it:
1
2
3
4
5
6
llm4s.providers {
openai-main {
provider = "openai"
model = "gpt-4o-mini"
}
}
1
2
3
export OPENAI_API_KEY=sk-...
export LLM4S_PROVIDER=openai-main
sbt "samples/runMain org.llm4s.samples.cookbook.StructuredOutputRecipe --live"
Adding a recipe
A recipe is a good first contribution. Copy one of the files, give it its own RecipeInfo, add it to
Cookbook.apps in Recipe.scala, and write its page in docs/examples/cookbook/ with the program pasted in.
CookbookDocsSpec then tells you what is missing: the page, its sections, its place in the navigation, the links from
this page, the README and the examples index, or a program that differs from the source. For the samples that are
not recipes, see the examples index.
Table of contents
- Classify text into an enum
- Extract structured data from an email
- Summarise a long document
- Answer questions over a folder of files
- An agent that calls two tools
- A guardrailed chatbot
- Stream tokens to the terminal
- Fall back between providers
- Cache repeated calls
- Evaluate an answer with a judge
- Answer questions with keyword search
- Remember facts between turns
- Several agents in one graph