Skip to main content

Overview

The swarms.prompts module provides tools for creating, managing, and versioning prompts with comprehensive history tracking, autosaving capabilities, and integration with agent systems.

Prompt Class

A powerful class for managing prompts with version control, edit history, and autosaving features.

Constructor

str
default:"uuid.uuid4().hex"
Unique identifier for the prompt
str
default:"prompt"
Name of your prompt
str
default:"Simple Prompt"
Description of the prompt
str
required
The main content of the prompt (minimum 1 character)
str
default:"current_time"
Timestamp when the prompt was created
str
default:"current_time"
Timestamp when the prompt was last modified
int
default:"0"
Number of times the prompt has been edited
List[str]
default:"[]"
History of all prompt versions
bool
default:"False"
Enable automatic saving of the prompt
str
default:"prompts"
Folder path within WORKSPACE_DIR for autosaving
bool
default:"False"
Enable auto-generation of prompts
str
default:"workspace_dir"
Parent folder for the autosave folder
Any
default:"None"
Optional LLM instance for prompt generation

Methods

edit_prompt

Edit the prompt content and update version control.
str
required
The updated content of the prompt
Raises:
  • ValueError: If the new content is identical to the current content
Thread-safe: This method is safe for concurrent access.

rollback

Roll back the prompt to a previous version.
int
required
The version index to roll back to (0 is the first version)
Raises:
  • IndexError: If the version number is out of range

get_prompt

Return the current prompt content as a string.
str
The current prompt content

return_json

Return the prompt as a JSON string.
str
JSON representation of the prompt with all metadata

add_tools

Add tool schemas to the prompt content.
List[Callable]
required
List of callable functions to add as tools

Pre-built Prompt Templates

The module includes numerous pre-built prompt templates for common agent roles:

Finance Prompts

FINANCE_AGENT_PROMPT

Comprehensive prompt for financial analysis agents.
Prompt template for legal analysis and research agents.

Product & Operations

PRODUCT_AGENT_PROMPT

Prompt for product management agents.

OPERATIONS_AGENT_PROMPT

Prompt for operations management agents.

GROWTH_AGENT_PROMPT

Prompt for growth and marketing agents.

Development Prompts

CODE_INTERPRETER

Prompt for code interpretation and execution agents.

DOCUMENTATION_WRITER_SOP

Standard operating procedure for documentation writing agents.

Autonomous Agent Prompts

AUTONOMOUS_AGENT_SYSTEM_PROMPT

Base system prompt for autonomous agents.

get_autonomous_agent_prompt

Generate an autonomous agent prompt.

get_autonomous_agent_prompt_with_context

Generate an autonomous agent prompt with additional context.

Example: Complete Prompt Management

Best Practices

  1. Enable autosave: Always enable autosave for important prompts to prevent data loss
  2. Use descriptive names: Give prompts clear, descriptive names for easy identification
  3. Version control: Leverage the built-in version control to track prompt evolution
  4. Add context: Include detailed descriptions to document the prompt’s purpose
  5. Organize by folder: Use the autosave_folder parameter to organize prompts by category
  6. Regular rollbacks: Test different prompt versions using rollback functionality
  7. Tool integration: Use add_tools() to seamlessly integrate function calling

Thread Safety

The Prompt class implements thread-safe operations for:
  • Edit operations
  • Rollback operations
  • Autosaving
This makes it safe to use in concurrent environments and multi-agent systems.

Autosave Behavior

When autosave is enabled:
  1. Prompts are saved to {WORKSPACE_DIR}/{autosave_folder}/prompt-id-{id}.json
  2. Each edit automatically triggers a save
  3. Rollback operations also trigger saves
  4. The entire prompt state (including history) is preserved

Integration with Agents