Skip to content

fix: don't inject cache_breakpoint for non-Anthropic LLMs#5954

Open
felipebridge wants to merge 1 commit into
crewAIInc:mainfrom
felipebridge:fix/cache-breakpoint-non-anthropic
Open

fix: don't inject cache_breakpoint for non-Anthropic LLMs#5954
felipebridge wants to merge 1 commit into
crewAIInc:mainfrom
felipebridge:fix/cache-breakpoint-non-anthropic

Conversation

@felipebridge
Copy link
Copy Markdown

@felipebridge felipebridge commented May 27, 2026

Problem

_setup_messages unconditionally calls mark_cache_breakpoint() on every message regardless of the configured LLM provider. cache_breakpoint is an Anthropic-only extension — Groq, OpenAI-compatible endpoints, and other providers reject messages containing this key with a hard validation error:

BadRequestError: {"error":{"message":"Invalid request: extra fields not permitted"}}

This breaks crewAI for any non-Anthropic provider once the cache-breakpoint code path is triggered.

Fix

Added _llm_is_anthropic() that checks the model name, and gated mark_cache_breakpoint on that check. For every other provider the messages pass through unchanged — no behavior change for existing Anthropic users.

Summary by CodeRabbit

Bug Fixes

  • Improved LLM provider compatibility by fixing validation errors that occurred with non-Anthropic providers. Caching functionality now works reliably across different LLM services.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 27, 2026

📝 Walkthrough

Walkthrough

CrewAgentExecutor adds a provider-aware check for Anthropic models and applies cache breakpoint wrapping conditionally during prompt setup. A new _llm_is_anthropic() helper detects Anthropic LLMs, and _setup_messages now conditionally injects cache breakpoint markers only for Anthropic, avoiding provider validation errors.

Changes

Anthropic-conditional cache breakpoint

Layer / File(s) Summary
LLM provider detection and conditional cache breakpoint wrapping
lib/crewai/src/crewai/agents/crew_agent_executor.py
Added _llm_is_anthropic() helper to detect Anthropic models by inspecting model string. Updated _setup_messages to conditionally wrap system and user prompts with mark_cache_breakpoint only for Anthropic LLMs, replacing prior unconditional cache-breakpoint injection.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 A bunny hops through LLM land,
Where Anthropic requests now make a stand,
No more cache breaks for the rest,
Just Claude gets the special test! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: preventing cache_breakpoint injection for non-Anthropic LLMs, which directly addresses the core change in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/crewai/src/crewai/agents/crew_agent_executor.py`:
- Around line 167-170: _llm_is_anthropic can miss Anthropic models when the
model string has different casing or when self.llm is provided as a plain
string; update _llm_is_anthropic to handle both shapes and normalize case: if
self.llm is a string use it directly, otherwise get getattr(self.llm, "model",
"") and then lower() the model value before checking startswith("claude") or
"anthropic" in model to ensure consistent detection; reference the
_llm_is_anthropic function and the self.llm / model variable when making the
change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 02d3e9d0-8177-46d3-aa88-a99382cc9f1e

📥 Commits

Reviewing files that changed from the base of the PR and between c5ea415 and ec3afbf.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/agents/crew_agent_executor.py

Comment on lines +167 to +170
def _llm_is_anthropic(self) -> bool:
"""Return True if the configured LLM is an Anthropic model."""
model: str = getattr(getattr(self, "llm", None), "model", "") or ""
return model.startswith("claude") or "anthropic" in model
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Harden Anthropic detection for case and string LLM refs.

_llm_is_anthropic() can false-negative when model casing isn’t lowercase or when self.llm is a string ref (allowed by the field type). Normalizing and handling both shapes keeps behavior consistent for Anthropic users.

Suggested patch
 def _llm_is_anthropic(self) -> bool:
     """Return True if the configured LLM is an Anthropic model."""
-    model: str = getattr(getattr(self, "llm", None), "model", "") or ""
-    return model.startswith("claude") or "anthropic" in model
+    llm_ref = getattr(self, "llm", None)
+    model: str = (
+        llm_ref if isinstance(llm_ref, str) else getattr(llm_ref, "model", "")
+    ) or ""
+    model = model.lower()
+    return model.startswith("claude") or "anthropic" in model
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/agents/crew_agent_executor.py` around lines 167 - 170,
_llm_is_anthropic can miss Anthropic models when the model string has different
casing or when self.llm is provided as a plain string; update _llm_is_anthropic
to handle both shapes and normalize case: if self.llm is a string use it
directly, otherwise get getattr(self.llm, "model", "") and then lower() the
model value before checking startswith("claude") or "anthropic" in model to
ensure consistent detection; reference the _llm_is_anthropic function and the
self.llm / model variable when making the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant