# CopilotKitAgentContext

> Angular standalone directive that shares application state with the agent as context from a template for @copilotkit/angular.

## Overview [#overview]

`CopilotKitAgentContext` is a standalone Angular directive that shares a piece of [context](https://docs.ag-ui.com/concepts/context) with the agent from your template. Apply it to any element. It adds a complete context when the element initializes, updates an existing registration when its inputs change, and removes it when the element is destroyed.

You can supply the context either as a single object via the directive selector input, or as separate `description` and `value` inputs. The context object takes precedence when both are provided.

This is the template-based alternative to [`connectAgentContext`](/reference/angular/functions/connectAgentContext).

## Import [#import]

The directive is standalone. Import the class into your component's `imports` array, then use the `copilotkitAgentContext` attribute selector in the template.

```ts title="src/app/profile.component.ts"
import { Component } from "@angular/core";
import { CopilotKitAgentContext } from "@copilotkit/angular";

@Component({
  selector: "app-profile",
  standalone: true,
  imports: [CopilotKitAgentContext],
  template: `
    <div copilotkitAgentContext
         description="User profile"
         [value]="profileJson">
    </div>
  `,
})
export class ProfileComponent {
  profile = { name: "Ada", plan: "pro" };
  profileJson = JSON.stringify(this.profile);
}
```

## Selector [#selector]

```
[copilotkitAgentContext]
```

It is an attribute directive, so apply it to any element.

## Inputs [#inputs]

<PropertyReference name="copilotkitAgentContext" type="Context">
  A complete context object containing both `description` and `value`. This is the directive's selector input (`[copilotkitAgentContext]="..."`). When provided, it takes precedence over the separate `description` and `value` inputs. A `Context` has the shape `{ description: string; value: string }`.
</PropertyReference>

<PropertyReference name="description" type="string">
  A human-readable description of what this context represents. Used to build the context object when the `copilotkitAgentContext` object input is not provided. The context is only registered when both `description` and `value` are set.
</PropertyReference>

<PropertyReference name="value" type="any">
  The value forwarded with `description` when the object input is not provided.
  The directive currently declares this input as `any`, but the AG-UI `Context`
  contract requires a string. Serialize objects before binding them. The context
  is registered only when both `description` and `value` are set during
  initialization.
</PropertyReference>

## Usage [#usage]

### Separate inputs [#separate-inputs]

```html
<div copilotkitAgentContext
     description="User preferences"
     [value]="userSettingsJson">
</div>
```

### Context object [#context-object]

Pass a complete `{ description, value }` object through the selector input.

```html
<div [copilotkitAgentContext]="contextObject"></div>
```

### Updating a registered context [#updating-a-registered-context]

The directive re-registers a context when inputs change after a complete context
was registered during initialization. Supply both fields at initialization.

```html
<div [copilotkitAgentContext]="formContext"></div>
```

If the initial object or separate value is missing, a later input change does
not create the first registration. Render the directive only after the complete
context is available, or use
[`connectAgentContext`](/reference/angular/functions/connectAgentContext) for a
reactive accessor.

## Behavior [#behavior]

- **Adds on init.** The context is registered with the agent in `ngOnInit`.
- **Updates an existing registration.** When `copilotkitAgentContext`,
  `description`, or `value` changes after initialization registered a context,
  the directive removes the previous context and adds the new one.
- **Does not add after an empty initialization.** If initialization has no
  complete context, later input changes do not create the first registration.
- **Removes on destroy.** The context is removed when the host element is destroyed.
- **Object input precedence.** When the `copilotkitAgentContext` object input is set, it is used and the separate `description` and `value` inputs are ignored. Otherwise, the context is built from `description` and `value`, and is only registered when both are set.
