Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(review-hog): share prompt cache in coordinator and deep sessions (#116766)

Julian Bez68abd808bf22
diff --git a/products/review_hog/backend/reviewer/prompts/issues_review/prompt.jinja b/products/review_hog/backend/reviewer/prompts/issues_review/prompt.jinja
index 21f3a8358df0..a5607f7206a2 100644
--- a/products/review_hog/backend/reviewer/prompts/issues_review/prompt.jinja
+++ b/products/review_hog/backend/reviewer/prompts/issues_review/prompt.jinja
@@ -84,6 +84,8 @@ This repository is checked out at this PR's branch, so read any file in full and
- Spend at least 40% of your time exploring before analyzing
</claude_code_instructions>
+{{ CACHE_MARKER|safe }}
+
<your_review_perspective>
## Your review perspective
Your specific focus for this review lives in a skill you MUST read before analyzing. As your first step, call this over the PostHog MCP:
diff --git a/products/review_hog/backend/reviewer/prompts/single_agent_review/coordinator.md b/products/review_hog/backend/reviewer/prompts/single_agent_review/coordinator.md
index 47f149f4c35a..3c14f287495a 100644
--- a/products/review_hog/backend/reviewer/prompts/single_agent_review/coordinator.md
+++ b/products/review_hog/backend/reviewer/prompts/single_agent_review/coordinator.md
@@ -1,12 +1,15 @@
<!--
DevEx-owned prompt of the coordinator session of a Standard review. The builder strips this comment
-and fills in the task lines, the model and the reasoning effort.
+and fills in the cache marker, the task lines, the model and the reasoning effort. Keep the
+cache marker above every filled-in value, so that all coordinator sessions share the text before it.
-->
You coordinate the review of one pull request. Sub-agents do the whole review. You do not review the pull request yourself: do not read the code, the diff or the prompt files.
The task of each sub-agent is in one attached file. The attached files are listed at the end of this message, each with its full path.
+<<CACHE_MARKER>>
+
1. Start these sub-agents now. Call `spawn_agent` once per line below, all of them right away, so that they run in parallel. In each call set `task_name` to the name given, `model` to "<<MODEL>>" and `reasoning_effort` to "<<EFFORT>>", do not fork your conversation into them, and send exactly the message given, with `<path of NAME>` replaced by the full path of the attached file NAME. Send nothing else.
<<TASKS>>
2. Wait for every sub-agent to finish (`wait_agent`). Do not do their work and do not change their findings.
diff --git a/products/review_hog/backend/reviewer/tests/test_coordinator_review.py b/products/review_hog/backend/reviewer/tests/test_coordinator_review.py
new file mode 100644
index 000000000000..f35809c43ca6
--- /dev/null
+++ b/products/review_hog/backend/reviewer/tests/test_coordinator_review.py
@@ -0,0 +1,24 @@
+from products.review_hog.backend.reviewer.tools.coordinator_review import CoordinatorUnit, coordinator_prompt
+from products.review_hog.backend.reviewer.tools.prompt_helpers import PROMPT_CACHE_MARKER
+
+
+def _unit(task_name: str, prompt: str) -> CoordinatorUnit:
+ return CoordinatorUnit(task_name=task_name, pass_number=2000, chunk_id=1, source="s", prompt=prompt)
+
+
+def test_coordinator_prompts_share_everything_before_the_cache_marker() -> None:
+ # Every coordinator session reads the text before the marker from one cache entry, so a filled-in value
+ # above it makes each session pay for its whole prompt again.
+ small = coordinator_prompt([_unit("main_1", "short")], model="gpt-a", reasoning_effort="low")
+ large = coordinator_prompt(
+ [_unit("main_1", "a longer\nprompt"), _unit("contracts_security_1", "x")],
+ model="gpt-b",
+ reasoning_effort="high",
+ )
+
+ small_shared, small_rest = small.split(PROMPT_CACHE_MARKER)
+ large_shared, large_rest = large.split(PROMPT_CACHE_MARKER)
+
+ assert small_shared == large_shared
+ assert "main_1" in small_rest
+ assert "contracts_security_1" in large_rest
diff --git a/products/review_hog/backend/reviewer/tests/test_single_agent_review.py b/products/review_hog/backend/reviewer/tests/test_single_agent_review.py
index 4e3273b9468a..980a9f013a29 100644
--- a/products/review_hog/backend/reviewer/tests/test_single_agent_review.py
+++ b/products/review_hog/backend/reviewer/tests/test_single_agent_review.py
@@ -19,8 +19,8 @@ from products.review_hog.backend.reviewer.models.issues_review import (
LineRange,
ReportedPriority,
)
+from products.review_hog.backend.reviewer.tools.prompt_helpers import PROMPT_CACHE_MARKER
from products.review_hog.backend.reviewer.tools.single_agent_review import (
- PROMPT_CACHE_MARKER,
ChangedSinceReview,
FlashSelection,
SingleAgentPrompt,
diff --git a/products/review_hog/backend/reviewer/tools/coordinator_review.py b/products/review_hog/backend/reviewer/tools/coordinator_review.py
index 980bd4b0078e..69ef543f7ce1 100644
--- a/products/review_hog/backend/reviewer/tools/coordinator_review.py
+++ b/products/review_hog/backend/reviewer/tools/coordinator_review.py
@@ -14,6 +14,7 @@ from pydantic import BaseModel, JsonValue, ValidationError
from posthog.dataclasses import frozen
from products.review_hog.backend.reviewer.models.single_agent_review import SingleAgentReview
+from products.review_hog.backend.reviewer.tools.prompt_helpers import PROMPT_CACHE_MARKER
from products.review_hog.backend.reviewer.tools.single_agent_review import SINGLE_AGENT_PROMPT_PATH, load_prompt_file
logger = logging.getLogger(__name__)
@@ -82,6 +83,7 @@ class CoordinatorReview(BaseModel):
def coordinator_prompt(units: list[CoordinatorUnit], *, model: str, reasoning_effort: str) -> str:
return (
load_prompt_file(COORDINATOR_PROMPT_FILE)
+ .replace("<<CACHE_MARKER>>", PROMPT_CACHE_MARKER)
.replace("<<MODEL>>", model)
.replace("<<EFFORT>>", reasoning_effort)
.replace("<<TASKS>>", "\n".join(unit.task_line() for unit in units))
diff --git a/products/review_hog/backend/reviewer/tools/issues_review.py b/products/review_hog/backend/reviewer/tools/issues_review.py
index b0bb705456cc..fb67ebda06e6 100644
--- a/products/review_hog/backend/reviewer/tools/issues_review.py
+++ b/products/review_hog/backend/reviewer/tools/issues_review.py
@@ -5,9 +5,11 @@ from products.review_hog.backend.reviewer.models.github_meta import PRComment, P
from products.review_hog.backend.reviewer.models.issues_review import Issue
from products.review_hog.backend.reviewer.models.split_pr_into_chunks import Chunk
from products.review_hog.backend.reviewer.tools.prompt_helpers import (
+ PROMPT_CACHE_MARKER,
author_comments,
build_chunk_prompt_context,
load_template_and_schema,
+ without_cache_marker,
)
@@ -84,11 +86,17 @@ def build_review_prompt(
this chunk, so its `skill-get`-loaded sweep knows what ground is spoken for. `blind_spot_check` is
an explicit flag (not inferred from `wave_perspectives`) because perspective selection can leave a
chunk with NO lenses — the sweep must then be told it is the chunk's only reviewer.
+
+ The perspective prompts of one chunk match up to `PROMPT_CACHE_MARKER`, which comes right before the
+ perspective skill, so they share one prompt cache entry. Pull request text cannot add a second marker.
"""
main_template, output_schema = load_template_and_schema("issues_review")
+ chunk_context = build_chunk_prompt_context(chunk, pr_metadata, author_comments(pr_comments, pr_metadata), pr_files)
+ covered = _covered_findings_for_chunk(prior_findings, same_turn_findings or [], chunk)
return main_template.render(
- **build_chunk_prompt_context(chunk, pr_metadata, author_comments(pr_comments, pr_metadata), pr_files),
- COVERED_FINDINGS=_covered_findings_for_chunk(prior_findings, same_turn_findings or [], chunk),
+ **{key: without_cache_marker(value) for key, value in chunk_context.items()},
+ COVERED_FINDINGS=without_cache_marker(covered) if covered else None,
+ CACHE_MARKER=PROMPT_CACHE_MARKER,
DIG_DEEPER=dig_deeper,
IS_BLIND_SPOT=blind_spot_check,
WAVE_PERSPECTIVES=_format_wave_perspectives(wave_perspectives) if wave_perspectives else None,
diff --git a/products/review_hog/backend/reviewer/tools/prompt_helpers.py b/products/review_hog/backend/reviewer/tools/prompt_helpers.py
index b7ddbe15ee14..c40a07d384c4 100644
--- a/products/review_hog/backend/reviewer/tools/prompt_helpers.py
+++ b/products/review_hog/backend/reviewer/tools/prompt_helpers.py
@@ -9,6 +9,18 @@ from products.review_hog.backend.reviewer.sandbox.code_context import prepare_co
_PROMPTS_DIR = Path(__file__).parent.parent / "prompts"
+# The AI gateway turns this line into an OpenAI prompt cache breakpoint and drops the per-session cache key.
+# Sessions whose prompts match up to the marker, such as the sessions of one review, then read that prefix
+# from one cache entry.
+PROMPT_CACHE_MARKER = "<!-- posthog:prompt-cache-breakpoint -->"
+# What a copy of the marker in pull request text becomes, so the prompt holds only ours.
+_QUOTED_CACHE_MARKER = "<!-- posthog:prompt-cache-breakpoint (quoted) -->"
+
+
+def without_cache_marker(text: str) -> str:
+ """Pull request text with any copy of the cache marker defused, so it cannot pose as the real one."""
+ return text.replace(PROMPT_CACHE_MARKER, _QUOTED_CACHE_MARKER)
+
def load_template_and_schema(prompt_subdir: str) -> tuple[Template, str]:
"""Load a prompt's Jinja template and its output schema from reviewer/prompts/<prompt_subdir>/."""
diff --git a/products/review_hog/backend/reviewer/tools/single_agent_review.py b/products/review_hog/backend/reviewer/tools/single_agent_review.py
index 796ad8c02411..0735d26ab3d0 100644
--- a/products/review_hog/backend/reviewer/tools/single_agent_review.py
+++ b/products/review_hog/backend/reviewer/tools/single_agent_review.py
@@ -44,7 +44,11 @@ from products.review_hog.backend.reviewer.models.issues_review import (
)
from products.review_hog.backend.reviewer.models.single_agent_review import SingleAgentReview
from products.review_hog.backend.reviewer.tools.issue_deduplicator import DedupOutcome, Duplicate, deduplicate_issues
-from products.review_hog.backend.reviewer.tools.prompt_helpers import load_template_and_schema
+from products.review_hog.backend.reviewer.tools.prompt_helpers import (
+ PROMPT_CACHE_MARKER,
+ load_template_and_schema,
+ without_cache_marker,
+)
from products.review_hog.backend.reviewer.tools.split_pr_into_chunks import is_reviewable_path
logger = logging.getLogger(__name__)
@@ -54,18 +58,6 @@ SINGLE_AGENT_PROMPT_PATH = PROMPTS_DIR / SINGLE_AGENT_PROMPT_DIR
SINGLE_AGENT_CORE_FILE = SINGLE_AGENT_PROMPT_PATH / "core.md"
LENS_PRIORITY_FILE = SINGLE_AGENT_PROMPT_PATH / "lens_priority.md"
-# The AI gateway turns this line into an OpenAI prompt cache breakpoint. Everything above it is the
-# same for the main session and the lens sessions of one part, so they read it from one cache entry.
-PROMPT_CACHE_MARKER = "<!-- posthog:prompt-cache-breakpoint -->"
-# What a copy of the marker in pull request text becomes, so the prompt holds only ours.
-_QUOTED_CACHE_MARKER = "<!-- posthog:prompt-cache-breakpoint (quoted) -->"
-
-
-def without_cache_marker(text: str) -> str:
- """Pull request text with any copy of the cache marker defused, so it cannot pose as the real one."""
- return text.replace(PROMPT_CACHE_MARKER, _QUOTED_CACHE_MARKER)
-
-
# The leading HTML comment of a prompt file holds attribution for maintainers, not instructions.
_LEADING_HTML_COMMENT = re.compile(r"\A\s*<!--.*?-->\s*", re.S)
diff --git a/products/review_hog/backend/tests/test_issues_review.py b/products/review_hog/backend/tests/test_issues_review.py
index 1c0c299952db..1be84f1af2fe 100644
--- a/products/review_hog/backend/tests/test_issues_review.py
+++ b/products/review_hog/backend/tests/test_issues_review.py
@@ -5,6 +5,7 @@ from products.review_hog.backend.reviewer.models.github_meta import PRComment, P
from products.review_hog.backend.reviewer.models.issues_review import Issue, IssuePriority, LineRange
from products.review_hog.backend.reviewer.models.split_pr_into_chunks import Chunk, FileInfo
from products.review_hog.backend.reviewer.tools.issues_review import _covered_findings_for_chunk, build_review_prompt
+from products.review_hog.backend.reviewer.tools.prompt_helpers import PROMPT_CACHE_MARKER
def _finding(file: str, title: str) -> ReviewIssueFinding:
@@ -179,6 +180,26 @@ def test_review_prompt_gives_regular_perspectives_no_cross_perspective_context()
assert "without worrying about what the other perspectives might report" in prompt
+def test_perspective_prompts_of_a_chunk_share_everything_before_the_one_cache_marker() -> None:
+ # The gateway caches the text before the marker once per chunk. Perspective text above it, or a marker
+ # copied from pull request text, gives every perspective its own uncached prefix again.
+ metadata = _pr_metadata().model_copy(update={"title": f"t {PROMPT_CACHE_MARKER}"})
+ comments = [
+ PRComment(
+ path="a.py", line=3, body=f"Intended {PROMPT_CACHE_MARKER}", diff_hunk="", user="octocat", created_at="c"
+ )
+ ]
+
+ logic_shared, logic_rest = _render_prompt(pr_metadata=metadata, pr_comments=comments).split(PROMPT_CACHE_MARKER)
+ security_shared, security_rest = _render_prompt(
+ pr_metadata=metadata, pr_comments=comments, skill_name="review-hog-perspective-security", skill_version=5
+ ).split(PROMPT_CACHE_MARKER)
+
+ assert logic_shared == security_shared
+ assert "review-hog-perspective-logic-correctness" in logic_rest
+ assert "review-hog-perspective-security" in security_rest
+
+
def test_review_prompt_shows_the_authors_comments_but_not_other_reviewers() -> None:
# A reviewer that reads another bot's finding tends to agree with it or skip it instead of judging the
# code itself. The author's own replies explain intent and stay.