{"record":{"id":"fde69e1af2648410","repo":"microsoft/autogen","slug":"agent-is-already-bound-to-a-different-id","errorCode":null,"errorMessage":"Agent is already bound to a different ID","messagePattern":"Agent is already bound to a different ID","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_base_agent.py","lineNumber":96,"sourceCode":"        return cls.internal_unbound_subscriptions_list\n\n    @property\n    def metadata(self) -> AgentMetadata:\n        assert self._id is not None\n        return AgentMetadata(key=self._id.key, type=self._id.type, description=self._description)\n\n    def __init__(self, description: str) -> None:\n        if AgentInstantiationContext.is_in_factory_call():\n            self._runtime: AgentRuntime = AgentInstantiationContext.current_runtime()\n            self._id = AgentInstantiationContext.current_agent_id()\n        if not isinstance(description, str):\n            raise ValueError(\"Agent description must be a string\")\n        self._description = description\n\n    async def bind_id_and_runtime(self, id: AgentId, runtime: AgentRuntime) -> None:\n        if hasattr(self, \"_id\"):\n            if self._id != id:\n                raise RuntimeError(\"Agent is already bound to a different ID\")\n\n        if hasattr(self, \"_runtime\"):\n            if self._runtime != runtime:\n                raise RuntimeError(\"Agent is already bound to a different runtime\")\n\n        self._id = id\n        self._runtime = runtime\n\n    @property\n    def type(self) -> str:\n        return self.id.type\n\n    @property\n    def id(self) -> AgentId:\n        return self._id\n\n    @property\n    def runtime(self) -> AgentRuntime:","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_base_agent.py#L78-L114","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nagent = MyAgent(\"d\")\nawait agent.bind_id_and_runtime(AgentId(\"t\", \"k1\"), runtime)\nawait agent.bind_id_and_runtime(AgentId(\"t\", \"k2\"), runtime)  # RuntimeError\n\n# after\nagent2 = MyAgent(\"d\")\nawait agent2.bind_id_and_runtime(AgentId(\"t\", \"k2\"), runtime)","handlingStrategy":"validation","validationCode":"if getattr(agent, \"_id\", None) is not None and agent._id != new_id:\n    raise RuntimeError(f\"agent already bound to {agent._id}\")\nawait agent.bind_id_and_runtime(new_id, runtime)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["autogen-core","agent-id","binding","api-misuse"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}