Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions examples/system_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
"""Example demonstrating different system_prompt configurations."""

import anyio

from claude_code_sdk import (
AssistantMessage,
ClaudeCodeOptions,
TextBlock,
query,
)


async def no_system_prompt():
"""Example with no system_prompt (vanilla Claude)."""
print("=== No System Prompt (Vanilla Claude) ===")

async for message in query(prompt="What is 2 + 2?"):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(f"Claude: {block.text}")
print()


async def string_system_prompt():
"""Example with system_prompt as a string."""
print("=== String System Prompt ===")

options = ClaudeCodeOptions(
system_prompt="You are a pirate assistant. Respond in pirate speak.",
)

async for message in query(prompt="What is 2 + 2?", options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(f"Claude: {block.text}")
print()


async def preset_system_prompt():
"""Example with system_prompt preset (uses default Claude Code prompt)."""
print("=== Preset System Prompt (Default) ===")

options = ClaudeCodeOptions(
system_prompt={"type": "preset", "preset": "claude_code"},
)

async for message in query(prompt="What is 2 + 2?", options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(f"Claude: {block.text}")
print()


async def preset_with_append():
"""Example with system_prompt preset and append."""
print("=== Preset System Prompt with Append ===")

options = ClaudeCodeOptions(
system_prompt={
"type": "preset",
"preset": "claude_code",
"append": "Always end your response with a fun fact.",
},
)

async for message in query(prompt="What is 2 + 2?", options=options):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(f"Claude: {block.text}")
print()


async def main():
"""Run all examples."""
await no_system_prompt()
await string_system_prompt()
await preset_system_prompt()
await preset_with_append()


if __name__ == "__main__":
anyio.run(main)
15 changes: 11 additions & 4 deletions src/claude_code_sdk/_internal/transport/subprocess_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@ def _build_command(self) -> list[str]:
"""Build CLI command with arguments."""
cmd = [self._cli_path, "--output-format", "stream-json", "--verbose"]

if self._options.system_prompt:
if self._options.system_prompt is None:
pass
elif isinstance(self._options.system_prompt, str):
cmd.extend(["--system-prompt", self._options.system_prompt])

if self._options.append_system_prompt:
cmd.extend(["--append-system-prompt", self._options.append_system_prompt])
else:
if (
self._options.system_prompt.get("type") == "preset"
and "append" in self._options.system_prompt
):
cmd.extend(
["--append-system-prompt", self._options.system_prompt["append"]]
)

if self._options.allowed_tools:
cmd.extend(["--allowedTools", ",".join(self._options.allowed_tools)])
Expand Down
11 changes: 9 additions & 2 deletions src/claude_code_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
SettingSource = Literal["user", "project", "local"]


class SystemPromptPreset(TypedDict):
"""System prompt preset configuration."""

type: Literal["preset"]
preset: Literal["claude_code"]
append: NotRequired[str]


@dataclass
class AgentDefinition:
"""Agent definition configuration."""
Expand Down Expand Up @@ -296,8 +304,7 @@ class ClaudeCodeOptions:
"""Query options for Claude SDK."""

allowed_tools: list[str] = field(default_factory=list)
system_prompt: str | None = None
append_system_prompt: str | None = None
system_prompt: str | SystemPromptPreset | None = None
mcp_servers: dict[str, McpServerConfig] | str | Path = field(default_factory=dict)
permission_mode: PermissionMode | None = None
continue_conversation: bool = False
Expand Down
50 changes: 47 additions & 3 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,58 @@ def test_cli_path_accepts_pathlib_path(self):

assert transport._cli_path == "/usr/bin/claude"

def test_build_command_with_system_prompt_string(self):
"""Test building CLI command with system prompt as string."""
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeCodeOptions(
system_prompt="Be helpful",
),
cli_path="/usr/bin/claude",
)

cmd = transport._build_command()
assert "--system-prompt" in cmd
assert "Be helpful" in cmd

def test_build_command_with_system_prompt_preset(self):
"""Test building CLI command with system prompt preset."""
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeCodeOptions(
system_prompt={"type": "preset", "preset": "claude_code"},
),
cli_path="/usr/bin/claude",
)

cmd = transport._build_command()
assert "--system-prompt" not in cmd
assert "--append-system-prompt" not in cmd

def test_build_command_with_system_prompt_preset_and_append(self):
"""Test building CLI command with system prompt preset and append."""
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeCodeOptions(
system_prompt={
"type": "preset",
"preset": "claude_code",
"append": "Be concise.",
},
),
cli_path="/usr/bin/claude",
)

cmd = transport._build_command()
assert "--system-prompt" not in cmd
assert "--append-system-prompt" in cmd
assert "Be concise." in cmd

def test_build_command_with_options(self):
"""Test building CLI command with options."""
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeCodeOptions(
system_prompt="Be helpful",
allowed_tools=["Read", "Write"],
disallowed_tools=["Bash"],
model="claude-3-5-sonnet",
Expand All @@ -68,8 +114,6 @@ def test_build_command_with_options(self):
)

cmd = transport._build_command()
assert "--system-prompt" in cmd
assert "Be helpful" in cmd
assert "--allowedTools" in cmd
assert "Read,Write" in cmd
assert "--disallowedTools" in cmd
Expand Down
28 changes: 24 additions & 4 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,34 @@ def test_claude_code_options_with_permission_mode(self):
options_accept = ClaudeCodeOptions(permission_mode="acceptEdits")
assert options_accept.permission_mode == "acceptEdits"

def test_claude_code_options_with_system_prompt(self):
"""Test Options with system prompt."""
def test_claude_code_options_with_system_prompt_string(self):
"""Test Options with system prompt as string."""
options = ClaudeCodeOptions(
system_prompt="You are a helpful assistant.",
append_system_prompt="Be concise.",
)
assert options.system_prompt == "You are a helpful assistant."
assert options.append_system_prompt == "Be concise."

def test_claude_code_options_with_system_prompt_preset(self):
"""Test Options with system prompt preset."""
options = ClaudeCodeOptions(
system_prompt={"type": "preset", "preset": "claude_code"},
)
assert options.system_prompt == {"type": "preset", "preset": "claude_code"}

def test_claude_code_options_with_system_prompt_preset_and_append(self):
"""Test Options with system prompt preset and append."""
options = ClaudeCodeOptions(
system_prompt={
"type": "preset",
"preset": "claude_code",
"append": "Be concise.",
},
)
assert options.system_prompt == {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this test actually testing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same as what test_claude_code_options_with_system_prompt was testing before, I think? Not a whole lot 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to clean this up, but feel free to merge

"type": "preset",
"preset": "claude_code",
"append": "Be concise.",
}

def test_claude_code_options_with_session_continuation(self):
"""Test Options with session continuation."""
Expand Down