fix: reorder extraction prompts for LLM prompt caching#1873
Open
hafezparast wants to merge 1 commit intounclecode:developfrom
Open
fix: reorder extraction prompts for LLM prompt caching#1873hafezparast wants to merge 1 commit intounclecode:developfrom
hafezparast wants to merge 1 commit intounclecode:developfrom
Conversation
Move instructions before URL/HTML content in all 4 extraction prompt templates. This enables LLM providers (Anthropic, OpenAI) to cache the instruction prefix across calls, reducing input token costs by up to 90% (Anthropic) or 50% (OpenAI) for batch extraction jobs. Before: URL → HTML → Instructions (instructions not cacheable) After: Instructions → URL → HTML (instructions cached as prefix) No behavioral change — LLMs produce identical output regardless of section ordering. Only the token billing is affected. Closes unclecode#1699 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Hahaha no way, this is why he's the goat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
prompts.pyto put instructions before URL/HTML contentWhat changed
crawl4ai/prompts.py— 4 templates reordered:PROMPT_EXTRACT_BLOCKSPROMPT_EXTRACT_BLOCKS_WITH_INSTRUCTIONPROMPT_EXTRACT_SCHEMA_WITH_INSTRUCTIONPROMPT_EXTRACT_INFERRED_SCHEMABefore:
After:
Why this works
LLM providers cache input token prefixes. When the same prefix appears across multiple requests, cached tokens are billed at a discount:
Since instructions are identical across pages in a crawl session but URL/HTML change every call, putting instructions first makes them a cacheable prefix.
Risk
Zero — LLMs produce identical output regardless of section ordering within the prompt. Only the token billing changes. All template variables (
{URL},{HTML},{REQUEST},{SCHEMA}) remain intact.Test plan
Closes #1699
🤖 Generated with Claude Code