Messaging System
Egregore’s messaging system provides universal message handling through ProviderThread containers and ContentBlocks. This architecture enables seamless communication with 30+ AI providers using a single, consistent interface.The Problem with Provider-Specific Formats
Traditional approaches require different message formats for each provider:Core Concepts
ProviderThread
A ProviderThread is a container that organizes messages into three categories:- SystemHeader - System instructions and configuration
- ProviderResponse - AI model responses
- ClientRequest - User messages
ProviderThread automatically formats messages for any provider. You work with a universal structure, and Egregore handles provider-specific conversion.
ContentBlocks
ContentBlocks are the atomic units of content within messages:The 3-Message-Type System
1. SystemHeader
System-level instructions that guide AI behavior:- One per thread
- Contains system instructions
- Never expires (permanent)
- Lives at depth -1 in PACT tree
2. ClientRequest
User messages sent to the AI:- Multiple per thread
- Contains user input
- Can include multimedia (images, audio, etc.)
- Stored at position 0 in PACT tree
3. ProviderResponse
AI model responses:- Multiple per thread
- Contains AI output
- Can include tool calls
- Stored at position 0 in PACT tree
ContentBlock Types
TextContent
Plain text messages:- User messages
- AI responses
- System instructions
ImageContent
Image data in various formats:- JPEG, PNG, GIF, WebP
- Base64 encoded or URLs
- Automatic provider-specific formatting
AudioContent
Audio files for speech-to-text or audio analysis:- MP3, WAV, M4A, FLAC
- Provider-dependent support
VideoContent
Video files for multimodal models:- MP4, MOV, AVI
- Provider-dependent support
DocumentContent
Documents like PDFs for document analysis:- PDF, DOCX, TXT
- Provider-dependent support
ToolCallContent
Function call requests from AI:- AI requests to execute tools
- Part of provider responses
- Automatically generated by providers
ToolResultContent
Function execution results:- Tool execution results
- Sent back to AI for synthesis
- Part of client requests
Message Flow
Basic Request-Response
Tool Call Flow
Multimedia Message Flow
ProviderThread API
Accessing Messages
Message Order
Messages are stored in chronological order:Historical Thread Access
Access thread state from past episodes:Provider-Specific Formatting
Automatic Format Conversion
Egregore handles conversion automatically:Format Methods
Providers implement format methods:You rarely need to call format methods directly. Egregore handles formatting automatically during
agent.call().Usage Tracking
Per-Message Tracking
Each message tracks token usage:Aggregate Usage
Track total usage across conversation:Multimodal Messages
Sending Images
Sending Multiple Content Types
Provider Support
Different providers support different content types:Message Persistence
Messages in PACT Context
Messages are stored in the PACT tree:Serialization
ProviderThread is PACT-compliant and serializable:Best Practices
Use ContentBlocks for structured data
Use ContentBlocks for structured data
ContentBlocks provide type safety and automatic formatting:
Check provider capabilities
Check provider capabilities
Verify provider support before using advanced features:
Access messages through thread
Access messages through thread
Use thread accessors instead of direct context access:
Monitor token usage
Monitor token usage
Track usage to control costs:
Common Patterns
Conversation with Context
Tool-Augmented Responses
Multimodal Analysis
Message History Export
Integration with Context System
Messages as PACT Components
Messages are stored in the context tree:Message Metadata
Messages can have additional context components:What’s Next?
Providers
Learn about the provider system and 30+ supported models
Multimedia Support
Deep dive into working with images, audio, and video
Context Management
Understand how messages fit into the PACT tree
Message API Reference
Complete API documentation for messaging system

