Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:ALwrity ALwrity BaseALwrityAgent

From Leeroopedia
Knowledge Sources
Domains Daily_Workflow, Agent_Framework, LLM
Last Updated 2026-10-24 00:00 GMT

Overview

Concrete tool for committee-based marketing agent generation using txtai LLM agents with safety frameworks and performance tracking, provided by the ALwrity daily workflow subsystem.

Description

The BaseALwrityAgent abstract class is the foundation of the committee-based agent framework (stage 3 of the Daily Workflow Generation pipeline). Each agent is initialised with a user ID, agent type (e.g. strategy_orchestrator), model name (default Qwen/Qwen3-4B-Instruct-2507), and optional tracing. It loads per-user agent profile overrides from the database (AgentProfile), builds a prompt context from onboarding data, and initialises a txtai Agent (with a fallback agent if txtai is unavailable). A safety framework is attached per user. Performance is tracked via the AgentPerformance dataclass (total/successful/failed actions, average response time, efficiency score). Class-level caches (_prompt_context_cache, _profile_cache) avoid redundant DB reads.

Usage

Import this base class when:

  • The daily workflow pipeline needs to generate tasks via a committee of specialised agents.
  • Per-user agent profiles (tone, system prompt, schedule) must override defaults.
  • MLflow tracing is needed for agent action observability.
  • A safety framework must gate agent actions.

Subclasses implement the agent-specific action logic.

Code Reference

Source Location

  • Repository: ALwrity
  • File: backend/services/agent_framework.py (L95-120+)

Signature

@dataclass
class AgentPerformance:
    """Performance metrics for an agent"""
    agent_id: str
    total_actions: int
    successful_actions: int
    failed_actions: int
    average_response_time: float
    success_rate: float
    last_action_at: str
    efficiency_score: float  # 0.0 to 1.0

class BaseALwrityAgent(ABC):
    """Base class for all ALwrity marketing agents."""

    _prompt_context_cache: Dict[str, Dict[str, Any]] = {}
    _profile_cache: Dict[str, Dict[str, Any]] = {}

    def __init__(
        self,
        user_id: str,
        agent_type: str,
        model_name: str = "Qwen/Qwen3-4B-Instruct-2507",
        llm: Any = None,
        enable_tracing: bool = True,
    ):
        """
        Args:
            user_id: Clerk user ID.
            agent_type: Agent category (e.g. 'strategy_orchestrator').
            model_name: HuggingFace model ID for txtai LLM.
            llm: Optional pre-built LLM (skips txtai auto-detection).
            enable_tracing: Enable MLflow tracing for agent actions.
        """

Import

from services.agent_framework import BaseALwrityAgent, AgentPerformance

I/O Contract

Inputs

Name Type Required Description
user_id str Yes Clerk user ID
agent_type str Yes Agent category (e.g. 'strategy_orchestrator')
model_name str No HuggingFace model ID (default Qwen3-4B-Instruct)
llm Any No Pre-built LLM (skips txtai auto-detection)
enable_tracing bool No Enable MLflow tracing (default True)

Outputs

Name Type Description
self.agent_id str Unique agent ID ({agent_type}_{user_id}_{timestamp})
self.txtai_agent txtai Agent Agent instance (or fallback)
self.performance AgentPerformance Performance tracker
self.safety_framework SafetyFramework Per-user safety gate

Usage Examples

Initialising a strategy orchestrator agent

from services.agent_framework import BaseALwrityAgent

# Subclass and implement action logic
class StrategyAgent(BaseALwrityAgent):
    def generate_tasks(self, plan_context):
        prompt = self._build_prompt(plan_context)
        return self.txtai_agent(prompt)

agent = StrategyAgent(
    user_id="clerk_user_123",
    agent_type="strategy_orchestrator",
    model_name="Qwen/Qwen3-4B-Instruct-2507",
    enable_tracing=True,
)
tasks = agent.generate_tasks(plan_context=daily_plan)
print(f"Generated {len(tasks)} tasks, success rate: {agent.performance.success_rate}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment