Skip to content

Commit ff425b2

Browse files
szwangclaude
andauthored
Add fallback model handling for parity with TypeScript SDK (#317)
Add support for automatic model fallback when primary model is overloaded. The Python SDK passes the fallback_model parameter to the Claude CLI, which handles the validation and fallback logic. Changes: - Add fallback_model parameter to ClaudeAgentOptions - Pass --fallback-model to CLI subprocess - Add test for fallback model command building The validation that fallback_model != model happens at the CLI layer, keeping the SDK implementation simple and focused on parameter passing. --------- Co-authored-by: Claude <[email protected]>
1 parent 5a4cc2f commit ff425b2

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/claude_agent_sdk/_internal/transport/subprocess_cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def _build_command(self) -> list[str]:
125125
if self._options.model:
126126
cmd.extend(["--model", self._options.model])
127127

128+
if self._options.fallback_model:
129+
cmd.extend(["--fallback-model", self._options.fallback_model])
130+
128131
if self._options.permission_prompt_tool_name:
129132
cmd.extend(
130133
["--permission-prompt-tool", self._options.permission_prompt_tool_name]

src/claude_agent_sdk/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ class ClaudeAgentOptions:
521521
max_budget_usd: float | None = None
522522
disallowed_tools: list[str] = field(default_factory=list)
523523
model: str | None = None
524+
fallback_model: str | None = None
524525
permission_prompt_tool_name: str | None = None
525526
cwd: str | Path | None = None
526527
cli_path: str | Path | None = None

tests/test_transport.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ def test_build_command_with_options(self):
131131
assert "--max-turns" in cmd
132132
assert "5" in cmd
133133

134+
def test_build_command_with_fallback_model(self):
135+
"""Test building CLI command with fallback_model option."""
136+
transport = SubprocessCLITransport(
137+
prompt="test",
138+
options=make_options(
139+
model="opus",
140+
fallback_model="sonnet",
141+
),
142+
)
143+
144+
cmd = transport._build_command()
145+
assert "--model" in cmd
146+
assert "opus" in cmd
147+
assert "--fallback-model" in cmd
148+
assert "sonnet" in cmd
149+
134150
def test_build_command_with_max_thinking_tokens(self):
135151
"""Test building CLI command with max_thinking_tokens option."""
136152
transport = SubprocessCLITransport(

0 commit comments

Comments
 (0)