> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/ai/generative-ai/large-language-models-llm/prompt-engineering/prompt-engineering-techniques/3.-knowledge-grounded-techniques/context-based-grounding-document-injection.md).

# Context-based grounding (document injection)

## About

Context-Based Grounding, also known as Document Injection, is a technique where **relevant documents or data are directly included inside the prompt** so the model can use them while generating a response.

Unlike RAG (which retrieves context dynamically), document injection assumes:

* You already have the required data
* You inject it directly into the prompt

Core idea:

> Provide the exact information the model should rely on during generation.

This technique turns the prompt into a **self-contained knowledge environment**, where both:

* The question
* The required data

exist together.

{% hint style="success" %}

* **Context-Based Grounding (Document Injection)** → *Providing data*
* **Source-Constrained Prompting** → *Restricting usage of data*
  {% endhint %}

## Why Context-Based Grounding Is Critical ?

Without injecting context:

* The model relies on general training data
* Domain-specific accuracy drops
* Internal systems are unknown to the model
* Hallucination risk increases

With document injection:

* Responses align with provided data
* Domain knowledge becomes available instantly
* No need for external retrieval systems
* Behavior becomes more predictable

This is especially useful when:

* Data is already available at runtime
* You want full control over context
* Retrieval systems are not needed or not available

## The Purpose of Context-Based Grounding

This technique aims to:

1. Provide domain-specific knowledge directly
2. Improve factual accuracy
3. Reduce hallucination
4. Enable reasoning over custom data
5. Simplify architecture (no retrieval layer needed)

It transforms the model from:

Generic knowledge system → Context-aware system

## Where it Fits in the Prompt Lifecycle

```
User Query
      ↓
Context Injection (Document Injection)
      ↓
Reasoning (Model processes query + document)
      ↓
Output Generation
```

It acts as a **direct knowledge layer inside the prompt**.

## Different Context Injection Patterns

#### 1. Full Document Injection

* Entire document is added to the prompt
* Useful for small documents

Example:

* Policy documents
* API specs
* Config files

#### 2. Chunked Document Injection

* Large documents are split into smaller sections
* Only relevant chunks are injected

Improves efficiency and focus.

#### 3. Section-Based Injection

* Specific sections of documents are labeled and injected

Example:

Policy Section:\
...

Exceptions:\
...

Improves navigation within context.

#### 4. Multi-Document Injection

* Multiple documents are provided together

Example:

* API spec
* Business rules
* Validation guidelines

Requires clear separation and labeling.

## Common Mistakes

### 1. Injecting Too Much Data

Large context:

* Increases token usage
* Reduces model focus
* Causes important details to be ignored

Always inject only relevant information.

### 2. Poor Context Structuring

Unstructured data leads to:

* Confusion between sections
* Misinterpretation
* Missing relationships

Use:

* Labels
* Delimiters
* Clear formatting

### 3. No Instruction to Use Context

If you do not explicitly instruct:

* Model may ignore context
* May mix with prior knowledge

Always combine with:

“Use the provided context…”

### 4. Missing Context Boundaries

If context is not clearly separated:

* Model may confuse task and data
* Instructions may be treated as data

Use clear delimiters like:

### Context:

### ...

### 5. Ignoring Context Size Limits

LLMs have context window limits.

If exceeded:

* Important parts may be truncated
* Output becomes unreliable

Plan for chunking when needed.

## Sample Prompts

### Without Context-Based Grounding

```
Explain the validation rules for our transaction system.
```

Issue:

* Model does not know your system
* Output becomes generic or incorrect

### With Context-Based Grounding

```
Use the following document to answer the question.

Context:
---
Transaction Rules:
- Amount must be greater than 0
- Currency must be valid ISO code
- Status must be SUCCESS or FAILED
---

Question:
Explain the validation rules for the transaction system.
```

Benefits:

* Accurate, domain-specific output
* Reduced hallucination
* Controlled response


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/ai/generative-ai/large-language-models-llm/prompt-engineering/prompt-engineering-techniques/3.-knowledge-grounded-techniques/context-based-grounding-document-injection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
