What you will be able to do
- Name the standard components of a chain (query understanding, retriever, prompt template, LLM, output parser) and place each where it belongs in the input-to-output flow
- Choose semantic, keyword, hybrid or structured (Unity Catalog function or Genie) retrieval based on what the query looks like and where the answer lives
- Pick a schema-constrained output component when a downstream system, not a person, reads the result
- Decide whether a fixed chain fits, or whether the steps depend on intermediate results
Key concept
Chain — An ordered pipeline of components that takes a raw user input and turns it into the output the application needs. You pick each component by asking what the step before it hands over and what the step after it expects to receive.
1.The chain as an input-to-output pipeline
A chain exists because a model call alone rarely matches both ends of the problem. On one end the user types a question. On the other, a person or a downstream system needs an answer grounded in your data and shaped the way it expects. Every component in between closes part of that gap, and its position follows from what it consumes and what it produces.
At inference time, the standard RAG chain runs these components in order:
- Query understanding (optional) turns the raw user text into a retrieval query, for example by rewriting it or extracting keywords.
- Retriever embeds the retrieval query with the same embedding model used on the document chunks, ranks chunks by similarity, and returns the top results.
- Prompt template (prompt augmentation) combines the user's query with the retrieved context and instructions.
- LLM generates a response grounded in that context.
- Output parser (post-processing, optional) applies business logic, adds citations, or enforces a format on the raw response.
| Component | Takes in | Hands on |
|---|---|---|
| Query understanding | Raw user query, possibly with conversation history | A retrieval query, plus any extracted filters or keywords |
| Retriever | Retrieval query | Top-k relevant chunks |
| Prompt template | User query and retrieved chunks | An augmented prompt with instructions |
| LLM | Augmented prompt | Generated response |
| Output parser / post-processing | Raw LLM response | Refined text, citations, or a validated structure |
Order is fixed by the data flow, not by preference. Grounding context has to exist before the LLM sees the prompt, so when a chain needs product documentation chunks from a Databricks AI Search index (formerly Vector Search), the retriever goes immediately before the prompt template and LLM, and it returns only the top-k chunks. A parser cannot supply context because it runs after generation. Pasting the entire documentation set into the system prompt skips retrieval altogether, and the point of the retriever is to pick the few chunks that matter. How many chunks to retrieve and how to combine them with the query both have a large effect on answer quality.
Pick the retriever for the kind of data, too. A Unity Catalog function that checks a live orders database fetches data for a different question. It is not the component that grounds a documentation answer. Guardrails can sit anywhere in this chain: filtering requests, checking permissions before a data source is touched, and moderating what is generated.
Checkpoint 1 of 9· Put it in order
A support bot must answer product questions from documentation chunks in a Databricks AI Search index and return the answer with citations. Put its RAG chain components in the order they run at inference time.
- 1.Output parser applies business logic and adds citations to the raw response
- 2.Prompt template combines the user's query, the retrieved chunks and instructions
- 3.Retriever embeds the retrieval query and returns the top-k most similar chunks
- 4.LLM generates a response grounded in the augmented prompt
- 5.Query understanding rewrites the raw user text into a retrieval query
The data flow sets the order: each component needs what the one before it produces. The grounding chunks must be retrieved and placed in the prompt before the LLM runs. Post-processing such as adding citations can only work on a response that has already been generated.
“Understand the user's question. Retrieve supporting data. Augment the prompt with supporting data. Generate a response from an LLM using the augmented prompt.”Source: docs.databricks.com
Checkpoint 2 of 9· Exam question
A team is building a RAG-based customer support chain on Databricks. The chain must fetch the most relevant product documentation chunks from a Databricks AI Search (Vector Search) index before the LLM generates an answer. Which chain component should the team add immediately before the LLM call to supply this grounding context?
Correct answer: A — A retriever bound to the AI Search index, which embeds the user's question and returns the top-k matches
- A. A retriever connected to the AI Search index embeds the incoming question, runs a similarity search against the indexed documentation and passes the top-k chunks to the LLM. This is the retrieval step a RAG chain places directly before generation.
- B. An output parser works on the model's reply after generation, to validate or reshape it. It cannot fetch documentation from the index before the LLM call, so it supplies no grounding context.
- C. A prompt template with the whole documentation set pasted into the system prompt never queries the AI Search index. It sends the same bulk text on every call, which can exceed the context window and does not select the most relevant chunks.
- D. A Unity Catalog function tool that reads order and shipment records returns structured transactional data. It does not run a semantic search over the indexed product documentation, so it cannot provide the required grounding chunks.
Sources1
2.Query understanding when inputs vary in kind
If users phrase questions the same way your documents are written, the raw query can work as the retrieval query. Real inputs are messier: misspellings, follow-ups that only make sense with chat history, and terms that differ from the vocabulary in the documents. Query understanding is the first component of the chain. It reshapes the input before anything is retrieved.
It does three main jobs:
- Query rewriting: paraphrasing conversation history, correcting spelling, or swapping in synonyms so the query matches the documents' terms.
- Filter extraction: pulling time periods ("reports from 2023"), products, or places out of the query and passing them to the retriever as parameters.
- Intent classification: sorting the query into categories such as "product information", "troubleshooting" or "account management" before extracting entities and rewriting.
Add intent classification as a front-end component when the inputs vary in kind, because the intent decides which entities matter and how to rewrite the query. The techniques themselves are covered in Intent detection on user input. One LLM call can do all of this, but splitting classification, extraction and rewriting into separate calls adds latency and gives finer control over each step.
These components are coupled to their neighbours. A rewritten query only helps if the retriever is tuned for the new form. An extracted filter does nothing unless the data pipeline stored that metadata on each chunk and the retriever accepts the filter. If you add the front-end step without changing the components after it, retrieval gets no better.
Checkpoint 3 of 9· Check yourself
After adding an LLM step that pulls years such as "reports from 2023" out of user questions and passes them to the retriever as a filter, a team sees no change in retrieval quality. What is the most likely cause?
Query-understanding components are coupled to the components after them. An extracted filter does nothing unless the chunks carry that metadata from the data pipeline and the retriever accepts and applies the filter.
“Filter extraction must be done in conjunction with changes to both metadata extraction data pipeline and retriever chain components”Source: docs.databricks.com
Sources2
3.Choosing the retriever from the shape of the query
Two questions decide the retriever: where the answer lives, and what the query looks like. A RAG chain can query a vector store, a keyword search, or a SQL database. The supporting data can be unstructured (PDFs, wikis, documents) or structured (customer records, SQL transaction data, application APIs).
| Strategy | Matches on | Query shape it wins on | How it works |
|---|---|---|---|
| Semantic search | Shared concepts | "how do i turn my phone on?" against a section titled "toggling the power" | Embeddings in a continuous vector space |
| Keyword search | Shared words, scored by frequency | "what does model HD7-8D do?": specific, non-descriptive technical terms and product names | Token methods such as bag-of-words, TF-IDF, BM25 |
| Hybrid search | Both | "how do I turn on my HD7-8D?": concept plus exact term | Runs both, then combines with reciprocal rank fusion or a re-ranking model |
On Databricks, you set this choice on the retriever itself. VectorSearchRetrieverTool takes query_type as "ANN" for approximate nearest-neighbour semantic search or "HYBRID" for hybrid search. num_results is the top-k, and filters is where the output of filter extraction goes.
vs_tool = VectorSearchRetrieverTool(
index_name="catalog.schema.index_name", # Index name in the format 'catalog.schema.index'
num_results=5, # Max number of documents to return
columns=["primary_key", "text_column"], # List of columns to include in the search
filters={"text_column LIKE": "Databricks"}, # Filters to apply to the query
query_type="ANN", # Query type ("ANN" or "HYBRID").A query about a specific record or a current value calls for a different component. "Can you help me return my last order?" needs the order row from a database and the return policy from a document, so the system queries one source and retrieves from the other. A vector index answers from whatever the data pipeline last chunked and embedded. A structured tool runs its query against the table at the moment it is called. For inventory counts that change every few minutes, the right component is a Unity Catalog function tool that queries the live table. An index re-embedded once a day, or yesterday's counts pasted into the prompt, is the wrong choice.
How much of the query the agent writes decides which structured option to use:
- Unity Catalog SQL function, exposed through
UCFunctionToolkit: the SQL is written in advance and the agent supplies only the parameters, for example a customer ID. - Genie: users or agents ask open-ended natural-language questions over a curated set of tables, and Genie generates the SQL. Genie Spaces are now called Genie Agents.
- Databricks SQL MCP: the agent itself needs to write and run SQL against Unity Catalog tables.
from databricks_langchain import UCFunctionToolkit
from unitycatalog.ai.core.databricks import DatabricksFunctionClient
toolkit = UCFunctionToolkit(
function_names=[
"main.default.databricks_docs_vector_search"
],
client=DatabricksFunctionClient(),
)
tools = toolkit.toolsCheckpoint 4 of 9· Check yourself
Users of a parts-support assistant write questions like "why does my X-4410 pump keep stalling?". These mix a described problem with an exact part number. The VectorSearchRetrieverTool is set to query_type="ANN", and the returned chunks often cover similar problems on the wrong part. Which change fits this query shape?
Semantic (ANN) search matches on concepts, so it misses exact, non-descriptive terms such as part numbers. Queries that combine a concept with an exact term are served by hybrid search, which runs keyword and semantic search and combines the results.
“Customer support queries that combined both semantic and technical terms. Example: “how do I turn on my HD7-8D?””Source: docs.databricks.com
Checkpoint 5 of 9· Exam question
An agent needs to answer questions about current inventory counts that change every few minutes; a static vector search index over product documentation would quickly become stale. Which chain component should the team add so the agent can fetch this real-time data on demand?
Correct answer: A — A UCFunctionToolkit tool over a Unity Catalog function that queries the inventory table
- A.
UCFunctionToolkitexposes a Unity Catalog function as a tool the agent can call during a conversation. Each call runs the query against the inventory table, so the counts come back current instead of coming from a precomputed index. - B. A retriever can only return what the index already holds. With an hourly re-sync, the index would lag behind counts that change every few minutes, so it is still a precomputed snapshot and does not fetch data on demand.
- C. A prompt template that inserts counts from a nightly snapshot puts figures into the prompt that are already hours old. It cannot reflect changes made since the snapshot, and it runs no query at request time.
- D. An output parser works on text the model has already produced. It can check the format of an inventory figure, but it cannot retrieve current counts, so a model with no data access still has nothing accurate to report.
4.The prompt template and the LLM
The prompt template is where retrieved context becomes model input. It places the user's query and the retrieved chunks inside instructions that tell the model how to use each one, and often how to format the answer. Getting this template right through iteration is what prompt engineering means in a chain. That is why a template is chosen together with the retriever: it has to fit the number and shape of the chunks the retriever returns.
The LLM component is a choice of model and its parameters, made to balance performance, latency and cost for this specific step. A step that only classifies intent and a step that writes a grounded multi-paragraph answer can use different models, because each LLM call in the chain is its own component.
Checkpoint 6 of 9· Check yourself
In a RAG chain, an intent-classification step and the final grounded-answer step both call the same large model. Latency is too high, and the answers are good. Which component decision fits the chain?
Each LLM call in a chain is a separate component. You choose its model and parameters to balance performance, latency and cost for that step. A step that only classifies intent can use a cheaper, faster model than the step that writes the grounded answer.
“LLM: Selecting the most appropriate model (and model parameters) for your application to optimize/balance performance, latency, and cost.”Source: docs.databricks.com
5.The output side: who reads the result
The last component depends on who reads the output. If a person reads it, post-processing can be light: apply business rules, add citations, run moderation. If a downstream service parses it, the chain needs an output parser that enforces a fixed structure, and the model call should be constrained to produce that structure in the first place. Databricks recommends structured outputs for extraction across many documents, batch inference that needs a specified format, and turning unstructured data into structured data.
"Respond in JSON" is not a schema. Databricks chat models can return free text, unconstrained JSON objects (json_object, intended for when the schema is not known in advance), or JSON that matches a declared schema (json_schema with strict). Only the last one fixes the fields. A service that expects sentiment, confidence and key_phrases needs that schema declared and the response validated against it after the LLM call. Lowering the temperature makes text more consistent, but it does not create fields. Retrieving example JSON adds context, but it does not enforce anything. Simpler, flatter schemas produce higher-quality JSON. The request mechanics are covered in Structured outputs with response_format and JSON schema.
Function calling also produces structured output, but for a different purpose. The model emits a JSON object of arguments, your code calls the function, and the result goes back to the model. The model itself never calls the function. Use it when the output is an action to take. For pure conversion of data into structure, use structured outputs instead.
Checkpoint 7 of 9· Match them up
Match each way of producing the final output to the situation it fits at the end of a chain.
Tap a term, then the definition that fits it.
Who reads the output decides the last component. A person needs only light post-processing. A parser needs fixed fields, and only a declared JSON schema provides them, because unconstrained json_object output leaves the fields open. Function calling also produces structured output, but it is for actions your code takes, not for converting data.
“You can choose to generate text, unstructured JSON objects, and JSON objects that adhere to a specific JSON schema.”Source: docs.databricks.com
Checkpoint 8 of 9· Exam question
A generative AI application must return LLM responses as a JSON object with fixed fields (sentiment, confidence, key_phrases) so a downstream service can parse the result without regex post-processing. Which chain component should the team add after the LLM call to guarantee this structure?
Correct answer: A — An output parser that validates the model's reply against the field definitions and coerces types
- A. An output parser runs after the LLM call. It checks the raw reply against the defined fields and types and converts values where needed. The downstream service therefore receives
sentiment,confidenceandkey_phrasesin a parseable form, and a reply that cannot be fixed is rejected. - B. An example selector works before generation by adding sample outputs to the prompt. It can nudge the model toward the right layout, but it checks nothing after the call, so the fields and parseability of the reply are still not guaranteed.
- C. A prompt template, even one that names the three fields, only asks the model to comply. It runs before the call and does not validate the reply, so a malformed or incomplete response can still reach the downstream service.
- D. A second model call at temperature 0 is still free-form generation. It makes the layout more consistent but does not validate the fields or types, so malformed output is still possible. It adds latency and cost without an enforced guarantee.
6.Fixed transformation or steps that depend on results
The last decision is whether the components run in a fixed order or whether the model chooses them. The full range of design patterns is covered in Deterministic chain vs single agent vs multi-agent supervisor. Here the test is the desired output. If it is a fixed transformation (retrieve, augment, generate, parse, the same way every time), a deterministic chain fits, because the developer sets the components, their order and their parameters. If the output is an action whose next step depends on what an earlier step returned, such as retrying an order lookup after an "invalid order number" response, the LLM needs to choose which tool to call next. That calls for an agent, and the retrievers and function tools above become its tools.
Checkpoint 9 of 9· Check yourself
Which of these applications needs an agent instead of a deterministic chain to produce its output?
The order assistant's next step depends on what the lookup returned, so the LLM has to choose which tool to call next. That calls for an agent. The other three apply the same transformation every time, even when it has several steps, so a deterministic chain with developer-set components, order and parameters fits them.
“If that tool responds with "invalid order number," the agent can retry or prompt the user for the correct ID”Source: docs.databricks.com
Sources4
Sources
(8)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://docs.databricks.com/aws/en/agents/tutorials/ai-cookbook/fundamentals-inference-chain-ragOfficial docs
“The top (most similar) results are returned.”
↩︎ The chain as an input-to-output pipeline“determining how many chunks to retrieve in step 2 and how to combine them with the user's query in step 3”
↩︎ The chain as an input-to-output pipeline“The LLM's response may be processed further to apply additional business logic, add citations, or otherwise refine the generated text”
↩︎ The chain as an input-to-output pipeline“augmenting the user's query with the retrieved context, in a template that instructs the model how to use each component”
↩︎ The prompt template and the LLM“chain of steps that are invoked at inference time is commonly referred to as the RAG chain”
↩︎ Key concept - 2.
“Analyzing and transforming user queries to better represent intent and extract relevant information”
↩︎ Query understanding when inputs vary in kind“Filter extraction must be done in conjunction with changes to both metadata extraction data pipeline and retriever chain components”
↩︎ Query understanding when inputs vary in kind“Intent classification: Use an LLM to classify the user's query into predefined categories”
↩︎ Query understanding when inputs vary in kind“Selecting the most appropriate model (and model parameters) for your application to optimize/balance performance, latency, and cost.”
↩︎ The prompt template and the LLM“Query rewriting must be done in conjunction with changes to the retrieval component”
↩︎ Exam trap 3“Customer support where queries contain specific, non descriptive technical terms.”
↩︎ Exam trap 4“Runs BOTH a semantic search and keyword search, then combines the results.”
↩︎ Prediction“Customer support queries that combined both semantic and technical terms. Example: “how do I turn on my HD7-8D?””
↩︎ Checkpoint“LLM: Selecting the most appropriate model (and model parameters) for your application to optimize/balance performance, latency, and cost.”
↩︎ Checkpoint - 3.
“The user's request is used to query an outside knowledge base such as a vector store, keyword search, or SQL database.”
↩︎ Choosing the retriever from the shape of the query“RAG architecture can work with either unstructured or structured supporting data.”
↩︎ Choosing the retriever from the shape of the query“Understand the user's question. Retrieve supporting data. Augment the prompt with supporting data. Generate a response from an LLM using the augmented prompt.”
↩︎ Checkpoint - 4.
“The agent queries the order database to retrieve the relevant order and references a policy document.”
↩︎ Choosing the retriever from the shape of the query“the developer defines which tools or models are called, in what order, and with which parameters”
↩︎ Fixed transformation or steps that depend on results“The LLM adaptively decides which tools to use, when to make more LLM calls, and when to stop.”
↩︎ Fixed transformation or steps that depend on results“If that tool responds with "invalid order number," the agent can retry or prompt the user for the correct ID”
↩︎ Checkpoint - 5.
“Use the Databricks SQL MCP when your agent needs to write and run SQL against your Unity Catalog tables.”
↩︎ Choosing the retriever from the shape of the query“when the query is known ahead of time and the agent provides the parameters”
↩︎ Exam trap 1 - 6.
“Enables users to ask natural language questions to query enterprise data directly.”
↩︎ Choosing the retriever from the shape of the query“Genie Agents were formerly known as Genie Spaces.”
↩︎ Choosing the retriever from the shape of the query - 7.
“Batch inference tasks that require outputs to be in a specified format.”
↩︎ The output side: who reads the result“using a simpler JSON schema for JSON schema definitions results in higher quality JSON generation”
↩︎ The output side: who reads the result“You can choose to generate text, unstructured JSON objects, and JSON objects that adhere to a specific JSON schema.”
↩︎ Exam trap 2“You can choose to generate text, unstructured JSON objects, and JSON objects that adhere to a specific JSON schema.”
↩︎ Checkpoint - 8.
“The LLM itself does not call these functions”
↩︎ The output side: who reads the result