You paste a 500-page PDF into Claude 4.7. The API returns garbage. You burned credits and got hallucinations.
Claude 4.7 has a massive context window. But stuffing an entire legal document into the prompt causes the "Lost in the Middle" phenomenon. The model forgets clauses buried on page 250. I have tested this extensively, and here is how I fix it.
Context Pre-Filtering
If the user asks about indemnification, do not send the entire Master Services Agreement. Run a local TF-IDF or fast vector search to extract only the 5 pages containing "indemnify," "liability," and "hold harmless."
This cuts your token cost by 90% and gives Claude focused, relevant context to work with. If your pipeline relies on semantic document parsing for legal files, this pre-filtering step is even more critical.
XML Framing Architecture
Anthropic's models are tuned to recognize data encapsulated in XML tags. Use this to bound your filtered context:
<system_instructions>
You are a senior contract analyst. Answer the user's question using ONLY the provided document excerpts.
</system_instructions>
<document_excerpts>
<excerpt id="1" source="MSA_Page_42">
[FILTERED TEXT HERE]
</excerpt>
</document_excerpts>The result: Latency drops from 40 seconds to 4 seconds. Hallucinations disappear. Token costs plummet. This is how you deploy human capability multiplication at scale.
Need help optimizing your API usage? AI Workflow Repair Intake. For more cost optimization strategies, see the 90% token trick.