Overview
Junkie uses Agno’s Team orchestration pattern where a leader agent coordinates specialized sub-agents. Each user gets their own team instance cached in memory with LRU eviction.Team Creation
Teams are created per-user bycreate_team_for_user() in agent_factory.py:167-329:
Team Leader Pattern
The Team Leader is the orchestrator that:- Receives the user’s request
- Decides which sub-agents to delegate to
- Coordinates multi-agent workflows
- Synthesizes responses
- Manages conversation history and user memories
Leader Configuration
agent_factory.py:310-327
System Prompt Management
The team leader’s instructions come from Phoenix with fallback:agent_factory.py:126-147
Memory System
The team uses a dedicated memory model for fast memory processing:agent_factory.py:153-161
Features:
- User-specific memories stored in PostgreSQL
- Fast memory updates with Groq’s 120B model
- Shared across all conversations for a user
- Enables personalization and context retention
Team Cache (LRU)
Teams are cached per-user with Least Recently Used eviction:agent_factory.py:335-339
Cache Operations
Get or Create Team:agent_factory.py:358-424
Why LRU Cache?
- Performance - Avoid recreating teams for active users
- Statefulness - Teams maintain conversation history
- Resource Management - Limit memory usage with MAX_AGENTS
- Fair Eviction - Inactive users evicted first
Resource Cleanup
When a team is evicted, all resources are cleaned up:agent_factory.py:385-408
Session Management
Each Discord channel gets its own session:chat_handler.py:163-177
Session Storage:
- Stored in PostgreSQL table
agent_sessions - Maintains conversation flow per channel
- Enables multi-channel support per user
Database Configuration
Teams use async PostgreSQL for better throughput:agent_factory.py:87-97
URL Conversion
PostgreSQL URLs are converted to async format:agent_factory.py:60-81
Configuration Variables
MAX_AGENTS- Maximum teams in cache (default: varies by deployment)AGENT_HISTORY_RUNS- Number of previous runs to include in contextAGENT_RETRIES- Max retries for failed agent callsDEBUG_MODE- Enable debug loggingDEBUG_LEVEL- Debug verbosity level