Add --system-prompt-extras option to append file content to system prompt#4818
Add --system-prompt-extras option to append file content to system prompt#4818mykolarudenko wants to merge 4 commits intoAider-AI:mainfrom
Conversation
…ompt - Add new CLI argument --system-prompt-extras in 'Other settings' group - Read extras file dynamically before each LLM request in fmt_system_prompt() - Fail gracefully with warning if file is not found or unreadable - Support for CLI, YAML config, and environment variables Closes Aider-AI#4817
There was a problem hiding this comment.
Pull request overview
Adds support for injecting additional instructions into the system prompt by specifying a file whose contents get appended before LLM requests.
Changes:
- Added
--system-prompt-extrasCLI/config option to accept a file path. - Plumbed the option through
maininto coder creation. - Appended the file’s content during system prompt formatting.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| aider/args.py | Adds the new --system-prompt-extras argument. |
| aider/main.py | Passes args.system_prompt_extras into Coder.create(). |
| aider/coders/base_coder.py | Stores system_prompt_extras_file and appends file content during prompt formatting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
aider/coders/base_coder.py
Outdated
| # Add extra system prompt content from file if specified | ||
| if self.system_prompt_extras_file: | ||
| extras_content = self.io.read_text(self.system_prompt_extras_file) | ||
| if extras_content: | ||
| prompt = prompt + "\n\n" + extras_content.strip() | ||
|
|
There was a problem hiding this comment.
Appending extras inside fmt_system_prompt() causes the extras file to be read and appended for every call (main system prompt, each example message, system reminders, etc.). This can duplicate the extras multiple times per request and even inject it into non-system example messages. Consider reading the extras file once per request (eg when assembling main_sys / system messages in format_chat_chunks()) and appending it only to the actual system prompt content.
aider/coders/base_coder.py
Outdated
| if self.system_prompt_extras_file: | ||
| extras_content = self.io.read_text(self.system_prompt_extras_file) | ||
| if extras_content: | ||
| prompt = prompt + "\n\n" + extras_content.strip() |
There was a problem hiding this comment.
extras_content.strip() changes the file’s content (removes leading/trailing whitespace/newlines). Since the option is described as appending the file content, it’s safer to preserve the text exactly (or at most rstrip a single trailing newline) so users can control formatting precisely.
| prompt = prompt + "\n\n" + extras_content.strip() | |
| # Preserve extras content formatting; at most remove a single trailing newline | |
| if extras_content.endswith("\n"): | |
| extras_content = extras_content[:-1] | |
| prompt = prompt + "\n\n" + extras_content |
aider/coders/base_coder.py
Outdated
| if self.system_prompt_extras_file: | ||
| extras_content = self.io.read_text(self.system_prompt_extras_file) | ||
| if extras_content: | ||
| prompt = prompt + "\n\n" + extras_content.strip() |
There was a problem hiding this comment.
InputOutput.read_text() reports failures via tool_error; with the current placement this can emit an error message repeatedly on every prompt formatting when the file is missing/unreadable. To match the intended “warning but continue” behavior (and avoid spamming/inflating error counters), read silently and emit a tool_warning once (or throttle/reuse the last error).
| auto_copy_context=args.copy_paste, | ||
| auto_accept_architect=args.auto_accept_architect, | ||
| add_gitignore_files=args.add_gitignore_files, | ||
| system_prompt_extras_file=args.system_prompt_extras, |
There was a problem hiding this comment.
This new CLI/config option introduces new behavior in prompt construction; please add/extend tests to cover that the extras file content is appended to the system prompt and that missing/unreadable files only produce a warning without breaking requests.
Read the extras file once per request and append only to the main system prompt, avoiding duplication across example messages and system reminders.
Only remove a single trailing newline instead of strip() to let users control formatting precisely.
- test_system_prompt_extras_file: verify extras content is appended to system prompt - test_system_prompt_extras_file_missing: verify graceful handling of missing file - test_system_prompt_extras_file_reread_each_request: verify file is re-read dynamically
Summary
Add a new
--system-prompt-extrasCLI option that reads a file and appends its content to the system prompt before each LLM request.Motivation
Many LLMs treat system prompts and user messages differently. To ensure the model strictly follows instructions, they should be added to the system prompt rather than user messages. This option allows users to inject custom instructions into the system prompt via an external file.
Closes #4817
Changes
--system-prompt-extrasargument in the "Other settings" group, accepting a file pathsystem_prompt_extras_fileparameter to__init__fmt_system_prompt()to read the extras file and append its content to the system prompt on each callargs.system_prompt_extrastoCoder.create()Behavior
system-prompt-extras), and environment variables (AIDER_SYSTEM_PROMPT_EXTRAS)Usage Example