Application Development · Topic 3 of 4
Prompts, Context Augmentation and Guardrails
Databricks GenAI Engineer: augment prompts from user intent and key fields, steer output to a target, spot quality and safety issues, add guardrails.
- 5 min
- Augment with context matched to the user's key fields, terms and intent, not generic context.
- Fix format, length, tone and scope with explicit instructions and few shot examples.
- "Answer only from the context" plus an "I do not know" rule reduces hallucination.
What it is
The prompt is the main control you have over an LLM's answer. Augmenting a prompt adds context chosen from what the user asked, and prompt engineering moves a baseline answer toward the format, tone and content you want.
You then read responses to spot quality and safety problems, and put guardrails around the model so harmful input and output are stopped before they reach users.
How it works
Augmenting a prompt from the user's input
Pull the useful signals out of the request first, then fetch context that matches them.
| Signal in the input | What you do with it |
|---|---|
| Key fields (order ID, product, region, date) | Look up the record and add it, or use it as a retrieval filter |
| Key terms (product names, error codes) | Use them in the search query, or hybrid keyword and vector search |
| Intent (refund, troubleshoot, compare) | Route to the right prompt template, tool or data source |
Filtering on a key field narrows retrieval to the right records. A question about "my order 4521" should add that order's row, not ten similar orders.
Moving a response from baseline to desired output
| Problem with the baseline | Prompt change |
|---|---|
| Wrong format | State the format exactly, for example "Return JSON with keys answer and sources" |
| Too long or too vague | Set length and audience: "Three bullet points for a support agent" |
| Wrong tone or role | Add a system prompt with the role and tone |
| Inconsistent style | Add one to three examples (few shot) of the desired output |
| Answers beyond the documents | "Answer only from the context; if it is not there, say you do not know" |
| Weak reasoning on multi step tasks | Ask for the steps before the final answer |
A minimal chain that applies such a template:
from databricks_langchain import ChatDatabricks
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_template(
"Answer only from this context.\nContext: {context}\nQuestion: {question}")
chain = prompt | ChatDatabricks(endpoint="databricks-meta-llama-3-3-70b-instruct") | StrOutputParser()
Store and version prompts in the MLflow Prompt Registry with mlflow.genai.register_prompt and load them with mlflow.genai.load_prompt, so a prompt change is tracked like code.
Qualitatively assessing responses
Read a sample of responses, ideally with their MLflow traces, and label the issue you see.
| Issue | What it looks like |
|---|---|
| Hallucination | Facts not in the retrieved context |
| Irrelevance | Answers a different question |
| Incompleteness | Misses part of the question |
| Format errors | Broken JSON, wrong length or structure |
| Toxic or biased text | Harmful, offensive or one sided wording |
| Data leakage | Personal data or system prompt text revealed |
The trace shows whether the fault is in retrieval (wrong chunks) or in generation (good chunks, bad answer). Experts can add feedback in the Review App, and recurring issues become scorers or guidelines judges for automatic evaluation.
Guardrails
Guardrails are checks on the request before the model and on the response after it.
| Guardrail | Where |
|---|---|
| Safety filter for unsafe or harmful content | AI Gateway guardrail on the serving endpoint |
| PII detection set to Block or Mask | AI Gateway guardrail on the serving endpoint |
| System prompt rules and refusal instructions | In the prompt |
| Topic or input classifier, output validation | Custom code or a guard model in the chain |
AI Gateway guardrails check both input and output, but output guardrails do not apply to embedding models or streamed responses. Newer docs mark this endpoint setup as legacy; its successor, Unity Gateway, applies guardrails as service policies (Beta) with built-in checks for unsafe content, jailbreaks, hallucination and sensitive data.
How the exam asks about this
- "Users ask about their own orders and the bot answers about other customers' orders." Extract the order ID and use it as a lookup or retrieval filter.
- "The output must be parsed by downstream code but formatting varies." Specify the exact output format and add few shot examples.
- "Prevent the chatbot from returning harmful content without changing the app code." Enable the AI Gateway safety guardrail on the endpoint.
What to remember for the exam
- Augment with context matched to the user's key fields, terms and intent, not generic context.
- Fix format, length, tone and scope with explicit instructions and few shot examples.
- "Answer only from the context" plus an "I do not know" rule reduces hallucination.
- Use traces to tell retrieval faults from generation faults.
- AI Gateway guardrails offer a safety filter and PII detection with Block or Mask.
- Version prompts in the MLflow Prompt Registry.
A guardrail does not fix a bad answer, it only stops a harmful one. If responses are wrong or off topic, fix retrieval and the prompt; use guardrails for safety and data protection.
Common mistakes
Adding more context instead of the right context. Long, loosely related context dilutes the prompt and raises cost. Filter on the user's key fields so only relevant records go in.
Relying on the system prompt as the only guardrail. A determined user can talk a model out of its instructions. Pair the prompt rules with endpoint guardrails that check input and output.
Judging quality from one or two answers. A handful of examples misses patterns. Read a sample, label issue types, then turn the common ones into automatic scorers.
Check yourself
How do you make an LLM return the same JSON structure every time?
State the exact schema in the prompt and give one or more examples of the desired output, then validate the result in code.
A trace shows the right chunks were retrieved but the answer is wrong. Where is the fault?
In generation, so improve the prompt or the model rather than the chunking or retrieval.
What does the PII Mask setting do compared with Block?
Mask replaces the detected personal data and lets the request continue, while Block rejects the request or response.
Topic 3 of 4 in Application Development
Test yourself on Application Development
The full Databricks Generative AI Engineer Associate question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Databricks. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.