From f660af121e0102647f7d98e69dbbdd8853d3e7e0 Mon Sep 17 00:00:00 2001 From: Mohammed Alshoura Date: Wed, 17 Dec 2025 17:25:25 -0500 Subject: [PATCH 1/2] adding fallback to --system-prompt-file when --system-prompt is too long --- .../_internal/transport/subprocess_cli.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py index a4882db1..e29781cb 100644 --- a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py +++ b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py @@ -335,6 +335,28 @@ def _build_command(self) -> list[str]: # Check if command line is too long (Windows limitation) cmd_str = " ".join(cmd) + if len(cmd_str) > _CMD_LENGTH_LIMIT and "--system-prompt" in cmd: + # Handle large --system-prompt by switching to --system-prompt-file + try: + sp_idx = cmd.index("--system-prompt") + sp_value = cmd[sp_idx + 1] + if len(sp_value) > _CMD_LENGTH_LIMIT // 2: + temp_file = tempfile.NamedTemporaryFile( + mode="w", suffix=".txt", delete=False, encoding="utf-8" + ) + temp_file.write(sp_value) + temp_file.close() + self._temp_files.append(temp_file.name) + # Replace --system-prompt with --system-prompt-file + cmd[sp_idx] = "--system-prompt-file" + cmd[sp_idx + 1] = temp_file.name + logger.info( + f"System prompt length ({len(sp_value)}) exceeds limit. " + f"Using --system-prompt-file: {temp_file.name}" + ) + except (ValueError, IndexError) as e: + logger.warning(f"Failed to optimize system prompt length: {e}") + if len(cmd_str) > _CMD_LENGTH_LIMIT and self._options.agents: # Command is too long - use temp file for agents # Find the --agents argument and replace its value with @filepath From 7f3ddff1636fba16ec5da0d1568a9de51e4bc640 Mon Sep 17 00:00:00 2001 From: Mohammed Alshoura Date: Wed, 17 Dec 2025 17:35:33 -0500 Subject: [PATCH 2/2] removed unnecessary check --- .../_internal/transport/subprocess_cli.py | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py index e29781cb..fd22a2da 100644 --- a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py +++ b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py @@ -340,20 +340,19 @@ def _build_command(self) -> list[str]: try: sp_idx = cmd.index("--system-prompt") sp_value = cmd[sp_idx + 1] - if len(sp_value) > _CMD_LENGTH_LIMIT // 2: - temp_file = tempfile.NamedTemporaryFile( - mode="w", suffix=".txt", delete=False, encoding="utf-8" - ) - temp_file.write(sp_value) - temp_file.close() - self._temp_files.append(temp_file.name) - # Replace --system-prompt with --system-prompt-file - cmd[sp_idx] = "--system-prompt-file" - cmd[sp_idx + 1] = temp_file.name - logger.info( - f"System prompt length ({len(sp_value)}) exceeds limit. " - f"Using --system-prompt-file: {temp_file.name}" - ) + temp_file = tempfile.NamedTemporaryFile( + mode="w", suffix=".txt", delete=False, encoding="utf-8" + ) + temp_file.write(sp_value) + temp_file.close() + self._temp_files.append(temp_file.name) + # Replace --system-prompt with --system-prompt-file + cmd[sp_idx] = "--system-prompt-file" + cmd[sp_idx + 1] = temp_file.name + logger.info( + f"System prompt length ({len(sp_value)}) exceeds limit. " + f"Using --system-prompt-file: {temp_file.name}" + ) except (ValueError, IndexError) as e: logger.warning(f"Failed to optimize system prompt length: {e}")