Skip to content

feat(agents): add get_effective_system_prompt and stable instruction api#5841

Open
MoShiha wants to merge 3 commits into
crewAIInc:mainfrom
MoShiha:feat/stable-agent-instruction-api
Open

feat(agents): add get_effective_system_prompt and stable instruction api#5841
MoShiha wants to merge 3 commits into
crewAIInc:mainfrom
MoShiha:feat/stable-agent-instruction-api

Conversation

@MoShiha
Copy link
Copy Markdown

@MoShiha MoShiha commented May 17, 2026

Confirm role, goal, backstory, system_template, and prompt_template as stable public API (safe to read and write after construction) by updating their Field descriptions in BaseAgent.

Add Agent.get_effective_system_prompt() -> str, which returns the fully rendered system prompt as it would be sent to the LLM. Respects system_template and prompt_template overrides, and immediately reflects in-place writes to role, goal, and backstory.

This is the read/write seam that DSPy optimizers and other instrumentation tools need to inspect and update agent instructions without patching internals. See: #5818

Summary by CodeRabbit

  • New Features

    • Added the ability to preview the fully rendered system prompt that will be sent at execution time.
  • Documentation

    • Clarified agent instruction fields (role, goal, backstory) as stable public API and safe to read/write after construction.
  • Tests

    • Added tests verifying instruction fields are readable/writable and that the rendered system prompt is non-empty and reflects updates.

Review Change Stack

Confirm role, goal, backstory, system_template, and prompt_template as
stable public API (safe to read and write after construction) by updating
their Field descriptions in BaseAgent.

Add Agent.get_effective_system_prompt() -> str, which returns the fully
rendered system prompt as it would be sent to the LLM. Respects
system_template and prompt_template overrides, and immediately reflects
in-place writes to role, goal, and backstory.

This is the read/write seam that DSPy optimizers and other instrumentation
tools need to inspect and update agent instructions without patching internals.
See: crewAIInc#5818

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 77003bf1-8204-4b52-8ff4-ee1991e87b5d

📥 Commits

Reviewing files that changed from the base of the PR and between c56597f and 630466c.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/agent/core.py

📝 Walkthrough

Walkthrough

Adds Agent.get_effective_system_prompt(), expands BaseAgent field descriptions for role/goal/backstory as stable public API, and adds tests verifying read/write behavior and rendered system prompt content and template substitution.

Changes

Stable Agent Instruction API

Layer / File(s) Summary
Stable API field descriptions
lib/crewai/src/crewai/agents/agent_builder/base_agent.py
BaseAgent.role, .goal, and .backstory field descriptions expanded to explicitly document these as stable public API fields and clarify read/write safety and instrumentation behavior.
get_effective_system_prompt() implementation
lib/crewai/src/crewai/agent/core.py
Agent class gains get_effective_system_prompt() method that renders the execution-time system prompt via Prompts(...).task_execution() with tools disabled and returns the rendered system string or fallback prompt.
Instruction API validation tests
lib/crewai/tests/agents/test_agent_instruction_api.py
New test suite with fixture and tests validating instruction field readability, writeability, non-empty effective prompt, live reflection of field mutations, and template substitution without raw token leakage.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • crewAIInc/crewAI#5774: Changes the prompt-construction path used by the new get_effective_system_prompt().

Suggested labels

size/L

Poem

🐰 I sniff the prompt, all tidy and bright,
Role, goal, backstory—stable by night,
Tests hop around, ensuring no quirks,
Rendered system prompt does all the work,
A little rabbit cheers and takes flight.

🚥 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 accurately describes the two main additions: the new get_effective_system_prompt() method and the designation of instruction fields as stable API.
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

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/agent/core.py`:
- Line 1297: The call currently coerces None to False by using
bool(self.use_system_prompt); change it to pass self.use_system_prompt through
unchanged so semantics match the runtime execution path. Locate the call in
creawi.agent.core (around the code that builds the prompt/request where
use_system_prompt is passed) and replace bool(self.use_system_prompt) with
self.use_system_prompt so None remains None and behavior is identical to the
actual execution path.
🪄 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: e87848b7-a96a-4700-a5d6-02a915bb859a

📥 Commits

Reviewing files that changed from the base of the PR and between a95d267 and c56597f.

📒 Files selected for processing (3)
  • lib/crewai/src/crewai/agent/core.py
  • lib/crewai/src/crewai/agents/agent_builder/base_agent.py
  • lib/crewai/tests/agents/test_agent_instruction_api.py

Comment thread lib/crewai/src/crewai/agent/core.py Outdated
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.

2 participants