OpenRouterAgents
What it is
- A factory that dynamically generates
Agentsubclasses for OpenRouter models returned byOpenRouterAPIIntegration.list_models(). - Each generated agent class exposes a
New(...)classmethod to instantiate anAgentwired 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
Agentsubclasses, one per eligible OpenRouter model. - Filtering/eligibility:
- If
include_modelsis provided, only models whoseidis in that list are considered. - Skips models unless:
"text"is inmodel_data["architecture"]["input_modalities"], and"tools"is inmodel_data["supported_parameters"].
- If
- Returns dynamically created
Configuration/Dependencies
- Depends on:
OpenRouterAPIIntegration- Must implement
list_models(save_json=False)and return a list of model dicts.
- Must implement
OpenRouterModel- Must implement
get_model(model_id)to return a chat model object used byAgent.
- Must implement
naas_abi_core.services.agent.Agenttypes:Agent,AgentConfiguration,AgentSharedState.
- Logging via
naas_abi_core.logger.
Generated agents:
- Are subclasses of
Agentcreated viatype(...). - Provide:
- Class attributes:
name,description. New(agent_shared_state: Optional[AgentSharedState] = None, agent_configuration: Optional[AgentConfiguration] = None) -> Agent- If
agent_configurationis not provided, a defaultAgentConfiguration(system_prompt=...)is constructed using fields from the model dict:id,name,descriptioncontext_lengtharchitecture.modality,architecture.input_modalities,architecture.output_modalities,architecture.tokenizerpricing.prompt,pricing.completion
- If
agent_shared_stateis not provided, a newAgentSharedState()is created. - Instantiates the
Agentwith emptytoolsandagents, andmemory=None.
- If
- Class attributes:
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"insupported_parameterswill 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.