Skip to main content

OpenRouterAgents

What it is

  • A factory that dynamically generates Agent subclasses for OpenRouter models returned by OpenRouterAPIIntegration.list_models().
  • Each generated agent class exposes a New(...) classmethod to instantiate an Agent wired to a specific OpenRouter model.

Public API

  • class OpenRouterAgents(openrouter_integration: OpenRouterAPIIntegration, openrouter_model: OpenRouterModel)

    • Holds dependencies used to list OpenRouter models and instantiate chat models.
  • create_agents(include_models: list[str] | None = None) -> list[type[Agent]]

    • Returns dynamically created Agent subclasses, one per eligible OpenRouter model.
    • Filtering/eligibility:
      • If include_models is provided, only models whose id is in that list are considered.
      • Skips models unless:
        • "text" is in model_data["architecture"]["input_modalities"], and
        • "tools" is in model_data["supported_parameters"].

Configuration/Dependencies

  • Depends on:
    • OpenRouterAPIIntegration
      • Must implement list_models(save_json=False) and return a list of model dicts.
    • OpenRouterModel
      • Must implement get_model(model_id) to return a chat model object used by Agent.
    • naas_abi_core.services.agent.Agent types:
      • Agent, AgentConfiguration, AgentSharedState.
  • Logging via naas_abi_core.logger.

Generated agents:

  • Are subclasses of Agent created via type(...).
  • Provide:
    • Class attributes: name, description.
    • New(agent_shared_state: Optional[AgentSharedState] = None, agent_configuration: Optional[AgentConfiguration] = None) -> Agent
      • If agent_configuration is not provided, a default AgentConfiguration(system_prompt=...) is constructed using fields from the model dict:
        • id, name, description
        • context_length
        • architecture.modality, architecture.input_modalities, architecture.output_modalities, architecture.tokenizer
        • pricing.prompt, pricing.completion
      • If agent_shared_state is not provided, a new AgentSharedState() is created.
      • Instantiates the Agent with empty tools and agents, and memory=None.

Usage

from naas_abi_marketplace.applications.openrouter.agents.OpenRouterAgents import OpenRouterAgents
from naas_abi_marketplace.applications.openrouter.integrations.OpenRouterAPIIntegration import OpenRouterAPIIntegration
from naas_abi_marketplace.applications.openrouter.models.OpenRouterModel import OpenRouterModel

integration = OpenRouterAPIIntegration()
model_registry = OpenRouterModel()

factory = OpenRouterAgents(integration, model_registry)

# Create agent classes (optionally filter by model ids)
agent_classes = factory.create_agents(include_models=["anthropic/claude-3.5-sonnet"])

# Instantiate the first generated agent
if agent_classes:
AgentCls = agent_classes[0]
agent = AgentCls.New()
print(AgentCls.name, AgentCls.description)

Caveats

  • Only models that support text input and explicitly include "tools" in supported_parameters will produce agents; others are silently skipped.
  • Model dicts missing expected fields will still work, but default placeholders like "unknown" / "N/A" may appear in the autogenerated system prompt.
  • Errors during model listing or per-model agent generation are caught and logged; create_agents() returns whatever was successfully created.