Swarms System Prompt Manager (swarms-spm)
A lightweight, standalone Python library for managing hierarchical system prompts with support for global, primary, secondary, and numbered prompts. Perfect for AI agent systems, LLM applications, and multi-agent swarms.
Note: The package is installed as swarms-spm but imported as swarms_spm (Python package names cannot contain hyphens).
Features
- Hierarchical Prompt Management: Organize prompts in a clear hierarchy (Global → Primary → Secondary → Numbered)
- Global System Prompts: Set class-level prompts that automatically apply to all instances
- Instance-Level Overrides: Override global prompts on a per-instance basis
- Secondary Prompts: Add specialized prompts as children of primary prompts
- Numbered Prompts: Support for multiple numbered prompts (system_prompt_2, system_prompt_3, etc.)
- Zero Dependencies: Only uses Python standard library - no external dependencies
- Type Hints: Full type annotations for better IDE support and code clarity
- Standalone: Can be used independently or integrated with any AI framework
Installation
pip install swarms-spm
Or install from source:
git clone https://github.com/IlumCI/swarms-spm.git
cd swarms-spm
pip install -e .
Quick Start
from swarms_spm import SystemPromptManager
# Set a global system prompt (applies to all instances)
SystemPromptManager.set_global_system_prompt(
"You are a helpful AI assistant."
)
# Create a manager instance
manager = SystemPromptManager(
secondary_system_prompt="Always provide clear, structured responses."
)
# Assemble prompts hierarchically
combined_prompt = manager.assemble_system_prompts(
system_prompt="Your primary role is data analysis.",
system_prompts_n={
2: "Check for outliers and anomalies.",
3: "Format reports with clear sections."
}
)
print(combined_prompt)
Output:
[Global System Prompt]
You are a helpful AI assistant.
[System Prompt]
Your primary role is data analysis.
[Secondary System Prompt]
Always provide clear, structured responses.
[System Prompt 2]
Check for outliers and anomalies.
[System Prompt 3]
Format reports with clear sections.
Usage Guide
Setting Global System Prompts
Global system prompts are set at the class level and automatically apply to all instances:
from swarms_spm import SystemPromptManager
# Set global prompt (applies to all instances)
SystemPromptManager.set_global_system_prompt(
"You are a professional AI assistant."
)
# Get the current global prompt
current = SystemPromptManager.get_global_system_prompt()
print(current) # "You are a professional AI assistant."
# Clear the global prompt
SystemPromptManager.clear_global_system_prompt()
Creating Manager Instances
from swarms_spm import SystemPromptManager
# Use class-level global prompt
manager1 = SystemPromptManager(
secondary_system_prompt="Focus on accuracy."
)
# Override global prompt at instance level
manager2 = SystemPromptManager(
global_system_prompt="You are a creative writer.",
secondary_system_prompt="Use vivid descriptions."
)
Assembling Prompts
The assemble_system_prompts() method combines prompts in hierarchical order:
manager = SystemPromptManager(
secondary_system_prompt="Provide detailed explanations."
)
combined = manager.assemble_system_prompts(
system_prompt="You are a data analyst.",
system_prompts_n={
2: "Always verify data sources.",
3: "Include statistical context."
}
)
Prompt Hierarchy:
- Global System Prompt (highest priority)
- Checks instance-level first, then class-level
- System Prompt (primary prompt)
- Secondary System Prompt (child of system prompt)
- System Prompt N (numbered prompts, sorted by n)
Integration with Swarms
Perfect for use with the Swarms framework:
from swarms_spm import SystemPromptManager
from swarms import Agent
from swarms.structs.hiearchical_swarm import HierarchicalSwarm
# Set global prompt for all agents
SystemPromptManager.set_global_system_prompt(
"You are part of Swarms Corporation Operations."
)
# Get global prompt
global_prompt = SystemPromptManager.get_global_system_prompt()
# Create agents with hierarchical prompts
research_agent = Agent(
agent_name="Research-Analyst",
global_system_prompt=global_prompt,
system_prompt="You are a Research Analyst.",
secondary_system_prompt="Focus on comprehensive research.",
model_name="gpt-4o-mini",
max_loops=1,
)
analysis_agent = Agent(
agent_name="Data-Analyst",
global_system_prompt=global_prompt,
system_prompt="You are a Data Analyst.",
secondary_system_prompt="Focus on data analysis.",
model_name="gpt-4o-mini",
max_loops=1,
)
# Create hierarchical swarm
swarm = HierarchicalSwarm(
name="Operations",
agents=[research_agent, analysis_agent],
max_loops=1,
)
# Run task
output = swarm.run("Analyze market trends and provide recommendations.")
API Reference
SystemPromptManager
Class Methods
set_global_system_prompt(prompt: str) -> None
Set a global system prompt that applies to all instances.
Parameters:
prompt(str): The global system prompt to apply
Example:
SystemPromptManager.set_global_system_prompt("You are a helpful assistant.")
get_global_system_prompt() -> Optional[str]
Get the current global system prompt.
Returns:
Optional[str]: The current global system prompt, or None if not set
Example:
prompt = SystemPromptManager.get_global_system_prompt()
clear_global_system_prompt() -> None
Clear the global system prompt.
Example:
SystemPromptManager.clear_global_system_prompt()
Instance Methods
__init__(global_system_prompt: Optional[str] = None, secondary_system_prompt: Optional[str] = None)
Initialize a SystemPromptManager instance.
Parameters:
global_system_prompt(Optional[str]): Instance-level global prompt. If None, uses class-level global prompt.secondary_system_prompt(Optional[str]): Secondary system prompt to include in assembly.
Example:
manager = SystemPromptManager(
global_system_prompt="Custom global prompt",
secondary_system_prompt="Secondary instructions"
)
assemble_system_prompts(system_prompt: Optional[str] = None, system_prompts_n: Optional[Dict[int, str]] = None) -> str
Assemble system prompts in hierarchical order with formatted headers.
Parameters:
system_prompt(Optional[str]): The primary system prompt. Defaults to None.system_prompts_n(Optional[Dict[int, str]]): Dictionary of numbered prompts where keys are integers (n) and values are prompt strings. Defaults to None.
Returns:
str: Combined system prompt string with headers and separators. Returns empty string if no prompts are provided.
Example:
combined = manager.assemble_system_prompts(
system_prompt="Main prompt",
system_prompts_n={2: "Prompt 2", 3: "Prompt 3"}
)
Examples
Example 1: Basic Usage
from swarms_spm import SystemPromptManager
# Set global prompt
SystemPromptManager.set_global_system_prompt("You are helpful.")
# Create manager
manager = SystemPromptManager(
secondary_system_prompt="Be concise."
)
# Assemble
result = manager.assemble_system_prompts(
system_prompt="Answer questions."
)
print(result)
Example 2: Multiple Numbered Prompts
from swarms_spm import SystemPromptManager
manager = SystemPromptManager()
result = manager.assemble_system_prompts(
system_prompt="You are a data analyst.",
system_prompts_n={
2: "Check data quality.",
3: "Identify patterns.",
4: "Provide insights.",
1: "Verify sources." # Will be sorted: 1, 2, 3, 4
}
)
Example 3: Instance-Level Override
from swarms_spm import SystemPromptManager
# Set class-level global
SystemPromptManager.set_global_system_prompt("Default global prompt")
# Instance 1: Uses class-level global
manager1 = SystemPromptManager()
prompt1 = manager1.assemble_system_prompts(system_prompt="Prompt 1")
# Includes: "Default global prompt"
# Instance 2: Overrides with instance-level global
manager2 = SystemPromptManager(
global_system_prompt="Custom global prompt"
)
prompt2 = manager2.assemble_system_prompts(system_prompt="Prompt 2")
# Includes: "Custom global prompt"
Architecture
Prompt Hierarchy
flowchart TD
GlobalClass["Global System Prompt<br/>(Class-level)<br/><i>Set once, applies to all</i>"]
GlobalInstance["Global System Prompt<br/>(Instance-level)<br/><i>Can override class-level</i>"]
SystemPrompt["System Prompt<br/>(Primary)<br/><i>Main prompt</i>"]
SecondaryPrompt["Secondary System Prompt<br/><i>Child of primary</i>"]
NumberedPrompts["System Prompt 2, 3, 4, ...<br/><i>Numbered prompts (sorted)</i>"]
GlobalClass -->|"Can be overridden"| GlobalInstance
GlobalInstance --> SystemPrompt
SystemPrompt --> SecondaryPrompt
SecondaryPrompt --> NumberedPrompts
Design Principles
- Zero Dependencies: Only uses Python standard library
- Type Safety: Full type hints for better developer experience
- Flexibility: Support for multiple prompt hierarchies
- Simplicity: Clean, intuitive API
- Standalone: Can be used independently or integrated with any framework
Requirements
- Python >= 3.8
- No external dependencies (only uses
typingfrom standard library)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by the hierarchical prompt management needs in multi-agent systems
- Designed to work seamlessly with Swarms
Support
For issues, questions, or contributions, please open an issue on the GitHub repository.
Changelog
Version 1.0.0
- Initial release
- Support for global, primary, secondary, and numbered prompts
- Hierarchical prompt assembly
- Zero dependencies
Metadata
Release files for swarms-spm 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| swarms_spm-1.0.2.tar.gz | 11.6 kB | Without provenance | Details for swarms_spm-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| swarms_spm-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for swarms_spm-1.0.2-py3-none-any.whl |
Total release size: 20.3 kB
Release files / swarms_spm-1.0.2.tar.gz
| Download URL | swarms_spm-1.0.2.tar.gz |
|---|---|
| Size | 11.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b175009dd36988957e896bf8ea60ae8435fafdc35049dabb7629f87d9a088bc
|
|
BLAKE2b-256 checksum How to use checksums |
1177250d2d326b2582c85b2e7aa660a519f301f4a839ca9d35effbcb7d0b1400
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Release files / swarms_spm-1.0.2-py3-none-any.whl
| Download URL | swarms_spm-1.0.2-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9346887a498a78a95160a81f2560330618b07041f55893289e8d585ee3401156
|
|
BLAKE2b-256 checksum How to use checksums |
c2fede3a9fda517f1f0d3720d3c81cb4270b4622c0ef0dfb1679e879ac062214
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Security for swarms-spm 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.