@@ -5,9 +5,11 @@ description: >
55 push JSON-serializable shared state via useAgentContext({ description,
66 value }). Covers per-thread clone WeakMap, UseAgentUpdate filter,
77 ProxiedCopilotRuntimeAgent provisional agent, agent.addMessage /
8- setState / abortRun. useAgentContext has no agentId parameter — context
9- is intentionally global. For rare per-agent scoping, use
10- copilotkit.addContext({ agentId }).
8+ setState / abortRun. useAgent returns { agent }; run status comes from
9+ agent.isRunning (subscribe via UseAgentUpdate.OnRunStatusChanged).
10+ useAgentContext has no agentId parameter and context is always global —
11+ the core addContext API also ignores any agentId field, so per-agent
12+ context scoping is not supported.
1113type : framework
1214framework : react
1315library : copilotkit
@@ -49,7 +51,7 @@ export function ChatDriver({
4951 route: string ;
5052 userId: string ;
5153}) {
52- const { agent, isRunning } = useAgent ({
54+ const { agent } = useAgent ({
5355 agentId: " default" ,
5456 threadId: " main" ,
5557 updates: [
@@ -64,7 +66,8 @@ export function ChatDriver({
6466
6567 return (
6668 <div >
67- { isRunning ? " …thinking" : " idle" } — { agent .messages .length } messages
69+ { agent .isRunning ? " …thinking" : " idle" } — { agent .messages .length } { " " }
70+ messages
6871 </div >
6972 );
7073}
@@ -87,12 +90,17 @@ async function ask(text: string) {
8790### Subscribe only to run-status to reduce re-renders
8891
8992``` tsx
90- const { isRunning } = useAgent ({
93+ const { agent } = useAgent ({
9194 agentId: " default" ,
9295 updates: [UseAgentUpdate .OnRunStatusChanged ],
9396});
97+ const isRunning = agent .isRunning ;
9498```
9599
100+ ` useAgent ` returns ` { agent } ` only; ` isRunning ` lives on the agent
101+ itself. Subscribing to ` OnRunStatusChanged ` forces a re-render when the
102+ value flips, so reading ` agent.isRunning ` stays live.
103+
96104### Share app state with every agent run (global)
97105
98106``` tsx
@@ -103,31 +111,6 @@ const value = useMemo(
103111useAgentContext ({ description: " user cart + route" , value });
104112```
105113
106- ### Per-agent context (rare — escape hatch)
107-
108- ` useAgentContext ` is intentionally global. For the rare case where a
109- specific agent should see a different context, use the imperative API.
110-
111- ``` tsx
112- import { useCopilotKit } from " @copilotkit/react-core/v2" ;
113- import { useEffect , useMemo } from " react" ;
114-
115- const { copilotkit } = useCopilotKit ();
116- const value = useMemo (
117- () => ({ papers: researchState .papers }),
118- [researchState .papers ],
119- );
120-
121- useEffect (() => {
122- const id = copilotkit .addContext ({
123- description: " paper list" ,
124- value: JSON .stringify (value ),
125- agentId: " research" ,
126- });
127- return () => copilotkit .removeContext (id );
128- }, [copilotkit , value ]);
129- ```
130-
131114### Abort the run
132115
133116``` tsx
@@ -265,34 +248,37 @@ remove/re-add churn in the core context store.
265248
266249Source: ` packages/react-core/src/v2/hooks/use-agent-context.tsx:30-35 `
267250
268- ### MEDIUM — Expecting ` useAgentContext ` to accept ` agentId `
251+ ### MEDIUM — Expecting ` useAgentContext ` or ` copilotkit.addContext ` to scope context per agent
269252
270253Wrong:
271254
272255``` tsx
273256useAgentContext ({ agentId: " research" , description: " paper list" , value });
257+ // or the imperative form:
258+ copilotkit .addContext ({
259+ description: " paper list" ,
260+ value: JSON .stringify (value ),
261+ agentId: " research" ,
262+ });
274263```
275264
276265Correct:
277266
278267``` tsx
279- const { copilotkit } = useCopilotKit ();
280- useEffect (() => {
281- const id = copilotkit .addContext ({
282- description: " paper list" ,
283- value: JSON .stringify (value ),
284- agentId: " research" ,
285- });
286- return () => copilotkit .removeContext (id );
287- }, [copilotkit , value ]);
268+ // Context is global — every agent run sees every registered entry.
269+ useAgentContext ({ description: " paper list" , value });
270+
271+ // When only one agent should key off a value, branch inside its prompt
272+ // or tool logic instead of trying to scope the context entry.
288273```
289274
290- ` useAgentContext ` is intentionally global by design — context is "state of
291- the world" and flows to every agent. Per-agent scoping is the escape hatch
292- via ` copilotkit.addContext({ agentId }) ` . Most of the time you want the
293- ambient (global) version.
275+ Context is intentionally global and there is no per-agent scoping hook.
276+ ` useAgentContext ` has no ` agentId ` parameter, and ` copilotkit.addContext `
277+ destructures only ` { description, value } ` — any ` agentId ` passed is
278+ silently dropped. Treat context as "state of the world" that every agent
279+ sees.
294280
295- Source: ` packages/react-core/src/v2/hooks/use-agent-context.tsx ` (no ` agentId ` parameter)
281+ Source: ` packages/react-core/src/v2/hooks/use-agent-context.tsx ` (no ` agentId ` parameter); ` packages/core/src/core/context-store.ts:26-31 `
296282
297283### MEDIUM — Two components using the same ` (agentId, threadId) ` expecting isolation
298284
0 commit comments