Function Signature
Purpose
Uses the LLM to add related terms and synonyms that may help find more relevant documents. The expanded query is used bysearch/2 if present.
Parameters
The agent context from the pipeline
Options for the expand step
Options
Custom expander module or function (default:
Arcana.Agent.Expander.LLM)- Module: Must implement
expand/2callback - Function: Signature
fn question, opts -> {:ok, expanded} | {:error, reason} end
Custom prompt function
fn question -> prompt_string endOnly used by the default LLM expander.Override the LLM function for this step
Context Updates
The query with added synonyms and related terms. Set to
nil if expansion fails.Examples
Basic Usage
With Rewrite Step
Custom Expander Module
Inline Expander Function
Domain-Specific Expansion
API-Aware Expansion
Query Transformation Chain
Expansion builds on previous transformations:expanded_query → rewritten_query → question
Examples of Expansions
| Original | Expanded |
|---|---|
| ”GenServer" | "GenServer process server state callback OTP behavior" |
| "supervision tree" | "supervision tree supervisor child strategy restart" |
| "message passing" | "message passing send receive process communication" |
| "ETS" | "ETS Erlang Term Storage table memory cache” |
Custom Expander Behaviour
Telemetry Event
Emits[:arcana, :agent, :expand] with metadata:
When to Use
Useexpand/2 when:
- Users search with terse queries lacking context
- Your documents use varied terminology for the same concepts
- You want to improve recall at the cost of some precision
- Embeddings alone miss semantic variations
Best Practices
- Don’t over-expand - Too many terms dilute the query
- Domain-specific expansion - Use relevant synonyms for your domain
- Combine with rewrite - Clean input before expansion
- Test impact - Measure precision/recall changes
- Consider cost - LLM-based expansion adds latency and cost
Trade-offs
Benefits:- Improved recall (find more relevant documents)
- Handles terminology variations
- Catches synonyms and related concepts
- May reduce precision (more false positives)
- Adds LLM call latency
- Can over-expand simple queries
See Also
- rewrite/2 - Clean input before expansion
- decompose/2 - Break into sub-questions
- search/2 - Uses expanded query