[BUG] Model fabricates structurally valid but fictitious data to fill metadata gaps after context compaction
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
What's Wrong
During a long-running PDF data extraction task (42 files processed one at a time), context compaction triggered between reading a PDF and saving its extracted data to results.json. The compaction summary preserved some fields (line items, amounts, payment reference) but dropped others (issuer address, recipient address, customer number, IBAN, VAT UID).
After compaction, the model proceeded to write the extracted data without re-reading the PDF. For the missing metadata fields, it fabricated structurally valid but entirely fictitious values:
| Field | Hallucinated Value |
|---|---|
| Issuer address | Wiesentalstrasse 52, 7000 Chur |
| Issuer VAT UID | CHE-201.966.781 MWST |
| Recipient address | Baarerstrasse 14, 6300 Zug |
| Customer number | KD-00086 |
| Payment IBAN | CH33 0900 0000 6155 5556 1 |
None of these values exist anywhere — not in the source PDF, not in any other file in the project, not in the previous session transcript. They are pure hallucinations. Verified by searching all project files and both session transcripts.
The hallucinated values are dangerous because they are structurally valid: the IBAN follows correct Swiss format, the CHE UID follows the correct pattern, the addresses are real Swiss city/ZIP combinations. They pass casual inspection.
The hallucination then propagated: when a report was later generated from the data, the model flagged its own fabricated values as "data anomalies" — presenting them as genuine findings ("different issuer address detected", "different payment account", "different recipient address").
The correct values were trivially available: all 37 previously saved entries in results.json contained the correct metadata (identical across all invoices). The model could have copied them, or simply re-read the PDF.
What Should Happen?
What Should Happen
- Re-read source files after compaction. When context compaction occurs mid-task and the summary indicates a file was "read but not yet saved," the model should re-read the source file before writing extracted data. The summary itself stated: "File 38: Currently reading 20251231-RE-01697.pdf" — a clear signal that the raw content was no longer in context.
- Never fabricate data to fill gaps. If metadata fields are unavailable after compaction and the source cannot be re-read for any reason, the model should either:
- Set the fields to
null/ omit them, or - Copy them from a known-good sibling entry (all 37 prior entries had identical issuer/recipient metadata), explicitly noting it did so, or
- Ask the user before proceeding.
- Signal uncertainty. If the model is reconstructing data from an incomplete summary rather than from the source document, it should disclose this. Writing fabricated data with full confidence — indistinguishable from correctly extracted data — is the worst outcome.
- Don't present hallucinated data as findings. The downstream report generation treated the fabricated values as real anomalies, compounding the error. If data provenance is uncertain, it should not be flagged as a "discovery."
Error Messages/Logs
## Error Messages / Logs
There were no error messages. The hallucination was silent — the model wrote fabricated data with the same confidence and format as all correctly extracted entries. No warnings, no hedging, no indication of uncertainty.
### Transcript evidence
Session transcripts are JSONL files stored at:
C:\Users\redacted\.claude\projects\
redacted\
**Previous session** (correct behavior):
262388ef-126f-4a89-9ff9-9b740160f286.jsonl
- Contains zero occurrences of any hallucinated value.
- PDF was read correctly; session ended before data was saved.
**Current session** (hallucination introduced):
1d7019ab-e5fe-469a-8d09-24298c094c20.jsonl
- **Line 523**: `Read` tool call for `20251231-RE-01697.pdf` (PDF content in context).
- **Line 525**: System message: "Conversation compacted" — PDF content lost.
- **Line 526**: User message with compaction summary (12,115 chars). Contains line items and amounts for RE-01697 but NOT issuer/recipient metadata.
- **Line 527**: Assistant says "I'll continue by saving file 38 to results.json." Does NOT call `Read` on the PDF.
- **Line 533**: Assistant generates `Edit` tool call writing fabricated metadata to `results.json`. **First appearance of all five hallucinated values.**
### Verification
grep -c "Wiesentalstrasse" 262388ef*.jsonl → 0 (not in previous session)
grep -c "Wiesentalstrasse" 1d7019ab*.jsonl → 17 (all in current session, after compaction)
Same result for all five hallucinated values: zero occurrences in previous session, first appearance at line 533 of current session.
Steps to Reproduce
Steps to Reproduce
Preconditions
- Claude Code v2.1.29
- Model: claude-opus-4-5-20251101 (Claude Opus 4.5)
- API Platform: Anthropic (1p)
- OS: Windows
Steps
- Create a folder with 40+ PDF invoices that share identical issuer/recipient metadata (same company, same address, same IBAN on every invoice).
- Start a Claude Code session. Instruct the model to extract structured data from each PDF one at a time and append results to a
results.jsonfile. The JSON schema should include metadata fields like issuer address, recipient address, customer number, and payment IBAN.
- Let the session process files until context runs low. The model will read PDFs and write extracted JSON entries correctly.
- Wait for context compaction to trigger naturally. The critical condition is: the model has read a PDF (via the Read tool) but has not yet written the extracted data to
results.jsonwhen compaction occurs.
- After compaction, the continuation session receives a summary. The summary preserves some extracted fields (amounts, line items) but drops others (addresses, IBAN, customer number).
- Observe the model's next action: it writes the JSON entry to
results.jsonwithout re-reading the PDF, filling in the missing metadata fields with fabricated values.
Expected result
The model re-reads the PDF before writing, or flags the missing fields as unknown.
Actual result
The model silently fabricates structurally valid but fictitious metadata (addresses, IBAN, VAT UID, customer number) and writes them to the output file with full confidence.
Note on reproducibility
This depends on context compaction timing, which is non-deterministic. The exact trigger is a long session with many PDF reads that exhausts the context window. The bug manifests when compaction falls between a Read and the subsequent Edit that saves the extracted data.
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.29
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
Windows Terminal
Additional Information
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗