0
0
Prompt Engineering / GenAIml~8 mins

Combining retrieved context with LLM in Prompt Engineering / GenAI - Model Metrics & Evaluation

Choose your learning style9 modes available
Metrics & Evaluation - Combining retrieved context with LLM
Which metric matters for this concept and WHY

When combining retrieved context with a large language model (LLM), the key metric is accuracy or relevance of the model's output. This is because the goal is to produce answers that correctly use the retrieved information. Metrics like precision and recall help measure how well the model uses the right context without adding wrong or irrelevant details.

For example, if the model retrieves documents to answer a question, precision measures how many retrieved facts are actually correct in the answer, while recall measures how many correct facts from the documents are included. Balancing these ensures the LLM output is both accurate and complete.

Confusion matrix or equivalent visualization (ASCII)
Confusion Matrix for context usage in LLM output:

               | Predicted Relevant | Predicted Irrelevant |
---------------|--------------------|----------------------|
Actually Relevant |        TP = 80      |        FN = 20       |
Actually Irrelevant |       FP = 10      |        TN = 90       |

Total samples = 80 + 20 + 10 + 90 = 200

Calculations:
Precision = TP / (TP + FP) = 80 / (80 + 10) = 0.89
Recall = TP / (TP + FN) = 80 / (80 + 20) = 0.80
F1 Score = 2 * (0.89 * 0.80) / (0.89 + 0.80) ≈ 0.84

This shows the model retrieves mostly relevant context (high precision) and covers most relevant facts (good recall).
Precision vs Recall tradeoff with concrete examples

When combining retrieved context with an LLM, there is a tradeoff between precision and recall:

  • High precision, low recall: The model uses only very certain retrieved facts. This means fewer wrong details but may miss some important information. Good when you want very trustworthy answers.
  • High recall, low precision: The model tries to include all possible relevant facts, even if some are uncertain. This covers more information but risks adding wrong or irrelevant details. Useful when completeness is critical.

Example: For a medical question, high precision is important to avoid wrong advice. For a research summary, high recall helps include all relevant studies.

What "good" vs "bad" metric values look like for this use case

Good metrics:

  • Precision ≥ 0.85: Most retrieved context used is correct.
  • Recall ≥ 0.75: Most relevant context is included in the output.
  • F1 Score ≥ 0.80: Balanced precision and recall.

Bad metrics:

  • Precision < 0.5: Many irrelevant or wrong facts included.
  • Recall < 0.4: Many relevant facts missed.
  • F1 Score < 0.5: Poor balance, unreliable output.

These values depend on the application but generally, higher precision and recall mean better use of retrieved context with the LLM.

Metrics pitfalls
  • Accuracy paradox: High overall accuracy can be misleading if the dataset is imbalanced (e.g., many irrelevant facts). Precision and recall give a clearer picture.
  • Data leakage: If the LLM sees the answer during training, metrics will be unrealistically high.
  • Overfitting: The model may memorize retrieved context instead of understanding it, inflating precision but hurting generalization.
  • Ignoring context quality: Metrics assume retrieved context is correct; poor retrieval hurts final output regardless of LLM quality.
Self-check question

Your model combining retrieved context with an LLM has 98% accuracy but only 12% recall on relevant facts. Is it good for production? Why or why not?

Answer: No, it is not good. The very low recall means the model misses most relevant facts, so the output is incomplete. Even though accuracy is high, the model fails to use enough correct context, which can lead to poor or misleading answers.

Key Result
Precision and recall are key to measuring how well the LLM uses retrieved context; balanced high values indicate good model performance.