microsoft/autogen · error · RuntimeError
Agent is already bound to a different ID
Error message
Agent is already bound to a different ID
What it means
bind_id_and_runtime(id, runtime) assigns an identity to an agent. If the agent already has an _id (set either in __init__ during runtime factory instantiation or a prior bind) and the new id differs (compared with AgentId equality: same type and key), it raises RuntimeError. This prevents rebinding one agent instance to a different identity.
Source
Thrown at python/packages/autogen-core/src/autogen_core/_base_agent.py:96
return cls.internal_unbound_subscriptions_list
@property
def metadata(self) -> AgentMetadata:
assert self._id is not None
return AgentMetadata(key=self._id.key, type=self._id.type, description=self._description)
def __init__(self, description: str) -> None:
if AgentInstantiationContext.is_in_factory_call():
self._runtime: AgentRuntime = AgentInstantiationContext.current_runtime()
self._id = AgentInstantiationContext.current_agent_id()
if not isinstance(description, str):
raise ValueError("Agent description must be a string")
self._description = description
async def bind_id_and_runtime(self, id: AgentId, runtime: AgentRuntime) -> None:
if hasattr(self, "_id"):
if self._id != id:
raise RuntimeError("Agent is already bound to a different ID")
if hasattr(self, "_runtime"):
if self._runtime != runtime:
raise RuntimeError("Agent is already bound to a different runtime")
self._id = id
self._runtime = runtime
@property
def type(self) -> str:
return self.id.type
@property
def id(self) -> AgentId:
return self._id
@property
def runtime(self) -> AgentRuntime:View on GitHub (pinned to 027ecf0a37)
Solutions
- Create a fresh agent instance per binding instead of rebinding an existing one.
- If the id is already correct (set during factory instantiation), skip calling bind_id_and_runtime.
- Check agent.id first and only bind when unbound or identical.
Example fix
# before
agent = MyAgent("d")
await agent.bind_id_and_runtime(AgentId("t", "k1"), runtime)
await agent.bind_id_and_runtime(AgentId("t", "k2"), runtime) # RuntimeError
# after
agent2 = MyAgent("d")
await agent2.bind_id_and_runtime(AgentId("t", "k2"), runtime) Defensive patterns
Strategy: validation
Validate before calling
if getattr(agent, "_id", None) is not None and agent._id != new_id:
raise RuntimeError(f"agent already bound to {agent._id}")
await agent.bind_id_and_runtime(new_id, runtime) Prevention
- Bind each agent exactly once; create new instances for new IDs.
- Prefer factory registration so the runtime assigns IDs and you never call bind_id_and_runtime manually.
- Track construction path in code review: factory-instantiated agents already have _id set.
When it happens
Trigger: Calling await agent.bind_id_and_runtime(AgentId('a','1'), runtime) on an agent that was already instantiated by a runtime with AgentId('a','2'); manually binding the same agent object twice with different IDs; re-registering an existing agent instance under a new type/key.
Common situations: Caching agent instances and reusing them across registrations; test code that binds one agent to multiple runtimes/ids; subclass code that calls bind after the factory already set the id.
Related errors
- Agent is already bound to a different runtime
- Invalid agent type: {type}. Allowed values MUST match the re
- Invalid agent id: {agent_id}
- AgentInstantiationContext cannot be instantiated. It is a st
- AgentInstantiationContext.agent_id() must be called within a
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/fde69e1af2648410.
Report an issue: GitHub.