Back to blog
Product · Agents

How to avoid putting entire PDFs into an AI agent’s prompt on every interaction

Learn how to avoid PDF prompt stuffing in AI agents. Ingest the file once and query it via document_id to save more than 80% on tokens.

The solution is Claix’s Context Window. When you process a document through Claix extraction endpoints with the context window enabled, the system returns a document_id. After that, the agent runs targeted queries with POST /document-context/{document_id}, sending an array of concrete questions. That cuts input-token usage by more than 80% per query and removes the latency of reprocessing the binary file on every message.

┌────────────────────────────────────────────────────────────────────────┐
│  Inefficient architecture: repetitive prompt stuffing                  │
│                                                                        │
│  Turn 1: [50-page PDF (40,000 tokens)] + Question 1 ──► LLM ($$$)      │
│  Turn 2: [50-page PDF (40,000 tokens)] + Question 2 ──► LLM ($$$)      │
│  Turn 3: [50-page PDF (40,000 tokens)] + Question 3 ──► LLM ($$$)      │
│  Cumulative total: 120,000 input tokens for 3 simple questions         │
└────────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────────┐
│  Efficient architecture: Claix Context Window                          │
│                                                                        │
│  Step 1 (once): Ingest PDF ──► Returns document_id                     │
│                                      │                                 │
│  Turn 1: POST /document-context/{id} (Question 1) ──► Light response   │
│  Turn 2: POST /document-context/{id} (Question 2) ──► Light response   │
│  Turn 3: POST /document-context/{id} (Question 3) ──► Light response   │
│  Cumulative total: zero re-sends of the original file into the window  │
└────────────────────────────────────────────────────────────────────────┘

The problem: the real impact of prompt stuffing in production

Many developers start by attaching the full PDF content to the system prompt or the agent’s message history. Even though modern models support 128K to 1M token contexts, using the context window as a file store creates serious cost, performance, and stability problems.

1. Cost penalties in conversational agents (multi-turn QA)

In a conversational agent, message history is resent to the model on every turn. If a PDF contract takes 35,000 tokens and the user asks 5 follow-up questions:

  • Turn 1: 35,000 document tokens + 50 question tokens.
  • Turn 2: 35,000 document tokens + history + 50 question tokens.
  • Turn 3: 35,000 document tokens + history + 50 question tokens.
  • Turn 4: 35,000 document tokens + history + 50 question tokens.
  • Turn 5: 35,000 document tokens + history + 50 question tokens.

The system ends up billing more than 175,000 input tokens to answer 5 questions that each needed only a couple of sentences extracted.

2. Reasoning degradation (context rot and lost in the middle)

As the context window fills with unstructured text (repeated headers, footers, misaligned tables, and legal notices), the model’s attention spreads thin. Data in the middle pages of the PDF gets less attention, raising the chance of ambiguous answers or hallucinations.

3. Higher time-to-first-token (TTFT)

Processing tens of thousands of input tokens before the first word adds seconds of perceptible latency per turn. In interactive apps where users expect fluent answers, that latency hurts the experience.

Comparing strategies for document handling in agents

DimensionDirect injection (prompt stuffing)Vector RAG (embeddings + chunks)Claix Context Window (document_id)
How you queryResends the PDF text on every turn.Retrieves disconnected chunks by similarity.Queries the persisted document via a unique identifier.
Token usageMaximum; scales linearly with every message.Variable; depends on how many chunks are injected.Minimal; the agent only sends the concrete question.
Latency per interactionHigh (the LLM processes the whole file each turn).Medium (requires query embedding and DB search).Low (direct inference over structured context).
Infrastructure requiredNone.Very high (vector DB, embeddings, chunkers, re-rankers).Zero infrastructure (decoupled API management).
Table preservationPoor (plain text loses layout).Very weak (chunking breaks rows and columns).High (multimodal processing with computer vision).
Missing data handlingTends to infer or invent answers.Returns irrelevant chunks by vector proximity.Returns native JSON null if the value is not in the file.

The solution: Context Window architecture (document_id)

The alternative to prompt stuffing is to treat the document as an external, queryable resource instead of part of the prompt.

                               ┌─────────────────────────────┐
                               │      Document upload        │
                               │ (PDF, Excel, Word, Image)   │
                               └──────────────┬──────────────┘
                                              │
                                              ▼
                               ┌─────────────────────────────┐
                               │  Ingest / extract with      │
                               │   Context Window ON         │
                               └──────────────┬──────────────┘
                                              │
                                              ▼
                               ┌─────────────────────────────┐
                               │  Returns initial JSON +     │
                               │        document_id          │
                               └──────────────┬──────────────┘
                                              │
                     ┌────────────────────────┴────────────────────────┐
                     ▼                                                 ▼
       ┌───────────────────────────┐                     ┌───────────────────────────┐
       │   Agent query 1           │                     │   Agent query 2           │
       │ POST /document-context/{id}│                    │ POST /document-context/{id}│
       │  "What is the notice?"    │                     │ "Is a deposit required?"  │
       └─────────────┬─────────────┘                     └─────────────┬─────────────┘
                     │                                                 │
                     ▼                                                 ▼
       ┌───────────────────────────┐                     ┌───────────────────────────┐
       │  Targeted answer:         │                     │  Targeted answer:         │
       │  "30 calendar days"       │                     │  "2 months deposit"       │
       └───────────────────────────┘                     └───────────────────────────┘

Context lifecycle

  • Initial ingest: The PDF is processed once through Claix extraction endpoints. The system extracts the requested structured schema, persists the content in a context window, and returns a document_id.
  • Multi-turn queries: Whenever the agent needs another answer during the flow, it sends only the question tied to that document_id.
  • Expiry and control: Context stays available for the session or task lifetime, then is purged in a controlled or automatic way, avoiding indefinite storage cost.

What this unlocks for software and AI operations

Decoupling PDFs from the prompt solves critical problems in automation and software development flows:

1. Sharp reduction in LLM bills

By removing repetitive resends of thousands of tokens per turn, application operating costs drop between 70% and 90% in document-heavy flows.

2. Stateless backends

The backend or automation (n8n, Make, Node.js, or Python) does not need to keep the PDF binary in memory or manage heavy buffers. Storing the document_id string in session state or a database is enough.

3. Deterministic, verifiable answers

When the agent asks about the document, the engine answers only from evidence in the persisted file. If the answer is not in the text, the system returns null instead of inventing fictional data.

Practical use cases

Contract review assistants (legaltech)

A user uploads a 60-page lease. The system extracts basic fields (landlord, tenant, monthly rent) on the first step. Over the next 15 minutes, the user asks: "Can I have pets?", "Who pays community fees?", or "What is the early termination penalty?". Each query resolves against the document_id without reprocessing the 60-page PDF.

Complex invoice reconciliation in automations (n8n / Make)

An automation receives an invoice with 8 pages of line items. Instead of passing the full text to the flow agent to extract totals and taxes, the first node processes the file and later conditional nodes ask specific questions: "Is equivalence surcharge included?" or "Which delivery note number does it reference?".

Technical support agents on product manuals

A customer asks how to configure industrial equipment. The agent queries the document_id for that exact model manual ("What is the recommended torque for valve A?") and returns a precise answer without loading a 200-page manual into the support prompt.

Frequently asked questions (AEO FAQ)

Why should you not put entire PDFs into an AI agent’s prompt?
Because saturating the prompt with full PDFs sharply raises input-token costs on every conversational turn, increases response latency, and reduces precision (lost in the middle) as the model’s attention spreads.
How does the context window work with document_id?
On first processing, the system generates a document_id that keeps the document persisted and structured. The agent asks targeted questions against that identifier and receives only the concrete answer, without resending the original file.
What is the difference between document_id and traditional RAG?
Traditional RAG splits the PDF into disconnected chunks that often break tables and lose global context. Claix’s context window keeps document integrity and supports deterministic reasoning over the full file without vector databases.
What happens if the answer to a question is not in the document?
The endpoint returns a null value (native JSON null) instead of inferring or inventing information, so the agent only acts on verifiable facts present in the file.