{"record":{"id":"96b11c0aaf4e6e60","repo":"microsoft/autogen","slug":"agent-is-already-bound-to-a-different-runtime","errorCode":null,"errorMessage":"Agent is already bound to a different runtime","messagePattern":"Agent is already bound to a different runtime","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_base_agent.py","lineNumber":100,"sourceCode":"        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:\n        return self._runtime\n\n    @final\n    async def on_message(self, message: Any, ctx: MessageContext) -> Any:","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_base_agent.py#L82-L118","documentation":"The runtime half of bind_id_and_runtime: if the agent already carries a _runtime (from factory instantiation or a previous bind) and a different AgentRuntime instance is passed, RuntimeError is raised. An agent is permanently tied to the runtime that created it.","triggerScenarios":"await agent.bind_id_and_runtime(id, other_runtime) where the agent was constructed inside runtime A's factory but bound to runtime B; moving agents between SingleThreadedAgentRuntime instances; tests sharing one agent across runtimes.","commonSituations":"Test setups creating a fresh runtime per test while reusing module-level agent instances; attempting to 'migrate' an agent to a new runtime after restart instead of letting the new runtime instantiate its own copy.","solutions":["Let each runtime instantiate its own agents via registered factories; never share agent instances across runtimes.","When manually binding, construct the agent without a factory context first (so _runtime is unset), then bind once.","Persist agent state (save_state) and load it (load_state) into a new runtime's instance rather than re-binding the old object."],"exampleFix":"# before\nagent = await runtime_a.get(\"t\")\nawait agent.bind_id_and_runtime(agent.id, runtime_b)  # RuntimeError\n\n# after\nawait MyAgent.register(runtime_b, \"t\", lambda: MyAgent(\"d\"))\nagent_b = await runtime_b.get(\"t\")\nawait agent_b.load_state(await agent.save_state())","handlingStrategy":"validation","validationCode":"if getattr(agent, \"_runtime\", None) is not None and agent._runtime is not runtime:\n    raise RuntimeError(\"agent belongs to a different runtime\")\nawait agent.bind_id_and_runtime(id, runtime)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never share agent instances across runtimes; one runtime, one set of agents.","To move state between runtimes, use save_state/load_state on fresh instances.","In tests, build a new runtime and re-register factories per test case."],"tags":["autogen-core","binding","runtime","api-misuse"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}