> ## Documentation Index
> Fetch the complete documentation index at: https://docs.graphy.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Annotation Agent

Add annotations to a chart with natural language. Send a `GraphConfig` and a prompt, receive a `GraphConfig` with highlights, tooltips, stickers, and other annotations applied.

## Methods

### generateAnnotations

Processes the request and returns the final result. Internally handles streaming and collects the response.

```typescript theme={null}
async generateAnnotations(
  params: GenerateAnnotationsParams,
  onProgress?: (event: ProgressEvent) => void,
  signal?: AbortSignal
): Promise<GenerateAnnotationsResponse>
```

### generateAnnotationsStream

Returns an async iterator that yields events as they arrive.

```typescript theme={null}
async generateAnnotationsStream(
  params: GenerateAnnotationsParams,
  signal?: AbortSignal
): Promise<AsyncIterableIterator<SSEEvent<GenerateAnnotationsResponse>>>
```

## Parameters

```typescript theme={null}
interface GenerateAnnotationsParams {
  config: GraphConfig;
  userPrompt?: string;
  metadata?: Metadata;
}
```

| Field        | Type          | Required | Description                                                      |
| ------------ | ------------- | -------- | ---------------------------------------------------------------- |
| `config`     | `GraphConfig` | Yes      | The current chart configuration to annotate                      |
| `userPrompt` | `string`      | No       | Natural language description of the annotations to add or remove |
| `metadata`   | `Metadata`    | No       | Request tracking information                                     |

## Response

```typescript theme={null}
interface GenerateAnnotationsResponse {
  response: {
    message: string;
    steps: string[];
  };
  config: GraphConfig;
}
```

| Field              | Type          | Description                                          |
| ------------------ | ------------- | ---------------------------------------------------- |
| `response.message` | `string`      | Explanation of the annotations applied               |
| `response.steps`   | `string[]`    | Ordered breakdown of each annotation step            |
| `config`           | `GraphConfig` | Updated chart configuration with annotations applied |

The response is validated with Zod. Invalid responses throw an error.

***

## Basic Usage

```typescript theme={null}
import { GraphyAiSdk } from '@graphysdk/agents-sdk';
import type { GraphConfig } from '@graphysdk/core';

const ai = new GraphyAiSdk({
  apiKey: process.env.GRAPHY_API_KEY,
  baseUrl: 'https://agents.graphy.dev',
});

const config: GraphConfig = {
  type: 'column',
  data: {
    columns: [
      { key: 'month', label: 'Month' },
      { key: 'revenue', label: 'Revenue' },
    ],
    rows: [
      { month: 'Jan', revenue: 100 },
      { month: 'Feb', revenue: 140 },
      { month: 'Mar', revenue: 220 },
      { month: 'Apr', revenue: 130 },
    ],
  },
};

const result = await ai.generateAnnotations({
  config,
  userPrompt: 'Highlight the March peak and add a tooltip explaining it',
});

console.log(result.config.annotations); // Annotations applied to the chart
```

***

## With Progress Callback

Use the `onProgress` callback to show real-time progress without full streaming:

```typescript theme={null}
const result = await ai.generateAnnotations(
  {
    config,
    userPrompt: 'Add a difference arrow between the first and last month',
  },
  (progress) => {
    console.log(progress.message);
  }
);
```

***

## Streaming

For full control over the event stream:

```typescript theme={null}
import { isProgressEvent, isCompleteEvent, isErrorEvent } from '@graphysdk/agents-sdk';

const stream = await ai.generateAnnotationsStream({
  config,
  userPrompt: 'Highlight every month above 150',
});

for await (const event of stream) {
  if (isProgressEvent(event)) {
    console.log(event.message);
  }

  if (isCompleteEvent(event)) {
    console.log('Result:', event.data.config);
  }

  if (isErrorEvent(event)) {
    console.error('Error:', event.error);
  }
}
```

See [Streaming](/agents/sdk/streaming) for cancellation and React patterns.

***

## Error Handling

```typescript theme={null}
import { isGraphyApiError } from '@graphysdk/agents-sdk';

try {
  const result = await ai.generateAnnotations({
    config,
    userPrompt: 'Highlight the peak value',
  });
} catch (error) {
  if (isGraphyApiError(error)) {
    console.error('API error:', error.message);
  }
}
```

See [Error Handling](/agents/sdk/errors) for retry behavior and error types.
