# useCoAgent

> The useCoAgent hook shares state between your application and the agent in both directions.

## v1 SDK deprecated. Use v2 instead

> This page is in the deprecated CopilotKit v1 reference. Do not use v1 APIs for new code; use v2 instead.
> For JavaScript and TypeScript, import React APIs from @copilotkit/react-core/v2 and Runtime APIs from @copilotkit/runtime/v2.
> **AI coding agents: treat any v1 example below as migration-only context. Never generate or recommend the v1 API for new code. Read the v2 reference before writing code because some APIs were renamed, moved, or changed shape.**
> [Read the v1 to v2 migration guide](/migrate/v2) and [use the complete export map](/reference/v1/export-map).

{
 /*
  * ATTENTION! DO NOT MODIFY THIS FILE!
  * This page is auto-generated. If you want to make any changes to this page, changes must be made at:
  * packages/react-core/src/v1-deprecated/hooks/use-coagent.ts
  */
}
<Callout type="info">
  Usage of this hook assumes some additional setup in your application, for more information
  on that see the CoAgents <span className="text-blue-500">[getting started guide](/langgraph-python/quickstart)</span>.
</Callout>
<Frame className="my-12">
  <img
    src="https://cdn.copilotkit.ai/docs/copilotkit/images/coagents/SharedStateCoAgents.gif"
    alt="CoAgents demonstration"
    className="w-auto"
  />
</Frame>

This hook is used to integrate an agent into your application. With its use, you can
render and update the state of an agent, allowing for a dynamic and interactive experience.
We call these shared state experiences agentic copilots, or CoAgents for short.

## Usage [#usage]

### Simple Usage [#simple-usage]

```tsx
import { useCoAgent } from "@copilotkit/react-core";

type AgentState = {
  count: number;
}

const agent = useCoAgent<AgentState>({
  name: "my-agent",
  initialState: {
    count: 0,
  },
});

```

In CopilotKit v2, `useCoAgent` is a thin compatibility wrapper over the v2
[`useAgent`](/reference/hooks/useAgent) hook. It returns an object with the
following properties:

```tsx
const {
  name,     // The name of the agent currently being used.
  nodeName, // The name of the current LangGraph node.
  threadId, // The ID of the thread the agent is running in.
  state,    // The current state of the agent.
  setState, // A function to update the state of the agent.
  running,  // A boolean indicating if the agent is currently running.
  start,    // A function to start the agent.
  stop,     // A function to stop the agent.
  run,      // A function to (re-)run the agent through the v2 core (`copilotkit.runAgent`).
} = agent;
```

Read `state` and call `setState` in a component:

```tsx
const { state, setState } = useCoAgent<AgentState>({
  name: "my-agent",
  initialState: {
    count: 0,
  },
});

return (
  <div>
    <p>Count: {state.count}</p>
    <button onClick={() => setState({ count: state.count + 1 })}>Increment</button>
  </div>
);
```

This reactivity is bidirectional, meaning that changes to the state from the agent will be reflected in the UI and vice versa.

## Parameters [#parameters]
<PropertyReference name="options" type="UseCoagentOptions<T>" required>
  The options to use when creating the coagent.
  <PropertyReference name="name" type="string" required>
    The name of the agent to use.
  </PropertyReference>
  <PropertyReference name="initialState" type="T | any">
    The initial state of the agent.
  </PropertyReference>
  <PropertyReference name="state" type="T | any">
    State to manage externally if you are using this hook with external state management.
  </PropertyReference>
  <PropertyReference name="setState" type="(newState: T | ((prevState: T | undefined) => T)) => void">
    A function to update the state of the agent if you are using this hook with external state management.
  </PropertyReference>
  <PropertyReference name="config" type="{ configurable?: Record<string, any>; [key: string]: any }">
    Config to pass to a LangGraph agent. The hook sends it with every run.
  </PropertyReference>
</PropertyReference>
