# LLM Chat Sidebar for Annotators

Source: https://www.potatoannotator.com/docs/features/chat-support

The LLM chat sidebar is a panel on the annotation page where an annotator holds a multi-turn conversation with a language model about the item on screen. Potato adds the sidebar when the config enables the `chat_support` block, and the button that opens the sidebar reads **Ask AI** by default. The model receives the task name, the task description, the label names, and the item's text. By default, Potato instructs the model to help the annotator reason through the item and not to choose a label for the annotator. Potato saves every message in the annotator's `user_state.json`, grouped by item.

## The sidebar on the annotation page

Potato puts an **Ask AI** button with a chat icon in the right-hand group of the top bar. Clicking the button slides the sidebar in from the right edge of the page, and the annotation page moves left by the width of the sidebar. The sidebar is 380 pixels wide by default. The annotator types a question and presses Enter to send the question, or Shift+Enter to start a new line. Annotation keyboard shortcuts do not fire while the cursor is in the chat box. The sidebar starts below the top bar and keeps the configured width on every screen, so on a narrow phone screen the sidebar covers the right side of the annotation page.

Each conversation belongs to one item. When the annotator moves to another item, the sidebar loads the new item's earlier messages, or shows "Ask a question about the current annotation." for an item with no conversation yet. Returning to an item brings back the item's conversation. The model sees only the conversation about the current item and never the annotator's chats about other items. When a request to the model fails, the sidebar shows "Sorry, I encountered an error. Please try again." and Potato saves the error reply with the other messages.

## Configuration options

The `chat_support` block takes the keys in the table below. Add the following block to an existing config to run the sidebar on a local Ollama model:

```yaml
chat_support:
  enabled: true
  endpoint_type: ollama
  ai_config:
    model: llama3.2
    base_url: http://localhost:11434
    max_tokens: 500
  ui:
    max_history_per_instance: 20
```

Setting `max_tokens` to 500 caps each reply below the 800-token default and keeps answers to a few paragraphs. Setting `max_history_per_instance` to 20 sends at most the 20 most recent messages about the item with each new question. A shorter history keeps each request smaller. Request size matters for a slow local model and for a hosted model that charges per token.

| Key | Default | What the key controls |
|---|---|---|
| `enabled` | `false` | Turns the sidebar on. The value must be a boolean. |
| `endpoint_type` | none | The model provider. Potato requires the key once `enabled` is true, and accepts the ten names under [Endpoint types](#endpoint-types). |
| `ai_config` | `{}` | The model, the credentials, and the request settings. |
| `ui` | `{}` | The button text, the input placeholder, the sidebar width, and the history sent to the model. |
| `system_prompt` | built-in prompt | A mapping with one key, `template`, whose string replaces the built-in prompt. |

The `ai_config` mapping takes the keys below:

| Key | Default | What the key controls |
|---|---|---|
| `model` | the endpoint's own default | The model name. A value that is set must be a non-empty string. |
| `api_key` | none | The provider key. Potato's validator requires a key for `anthropic`, `gemini`, `huggingface`, and `openrouter`. The `openai` and `openai_vision` types also read `OPENAI_API_KEY` from the environment, and need no key when `base_url` points at a self-hosted OpenAI-compatible server. `anthropic_vision` also reads `ANTHROPIC_API_KEY`. |
| `base_url` | `http://localhost:11434` for `ollama` and `ollama_vision`, `http://localhost:8000` for `vllm` | The server address for a self-hosted model. |
| `temperature` | `0.1` | Sampling temperature, from 0 to 2. |
| `max_tokens` | `800` | The longest reply, in tokens. The value must be a positive integer. |
| `timeout` | per endpoint | Seconds to wait for a reply. The defaults are 30 for `openai`, `anthropic`, `gemini`, `huggingface`, `vllm`, and `openrouter`, 60 for `ollama`, `openai_vision`, and `anthropic_vision`, and 120 for `ollama_vision`. |

Write a key as `"${OPENAI_API_KEY}"` to keep the key out of the config file. Potato expands `${VAR}` references anywhere in the `chat_support` block when the server loads the config.

The `ui` mapping takes the keys below:

| Key | Default | What the key controls |
|---|---|---|
| `title` | `"Ask AI"` | The text on the top-bar button and in the sidebar header. |
| `placeholder` | `"Ask about this annotation..."` | The placeholder in the chat box. |
| `sidebar_width` | `380` | The sidebar width in pixels, an integer from 200 to 800. |
| `max_history_per_instance` | `50` | The most recent messages about the item sent with each new question. Each question and each reply counts as one message. The value must be a positive integer. |

The `system_prompt.template` string can use five variables, each in braces: `{task_name}`, `{task_description}`, `{annotation_labels}`, `{instance_text}`, and `{instance_id}`. A template that names any other variable in braces falls back to the built-in prompt, and the server log records a warning. Write a literal brace as `{{` or `}}`.

## Endpoint types

`chat_support.endpoint_type` accepts ten names:

- **Hosted:** `openai`, `anthropic`, `gemini`, `huggingface`, `openrouter`, `openai_vision`, and `anthropic_vision`
- **Self-hosted:** `ollama`, `ollama_vision`, and `vllm`

The list differs from the names `ai_support.endpoint_type` accepts in two places. First, `yolo` is missing, because a YOLO detector cannot answer a chat message. Second, OpenRouter takes only the spelling `openrouter`, and the older `open_router` fails validation under `chat_support`. A vision type lets the sidebar share a server and a model with [visual AI support](/docs/features/visual-ai-support) on an image task. The sidebar still sends text only, and the model never receives the item's image.

When the chat endpoint fails to start, Potato logs an error and turns the sidebar off until the server restarts. The annotation page then shows no **Ask AI** button. A server log line beginning "Chat endpoint initialized" confirms that the endpoint started, and an error line naming the chat endpoint gives the reason when the endpoint failed to start.

## Messages sent to the model

Each question goes to the model as a list of chat messages. The first message is the system prompt, filled in for the current item. The item's earlier messages follow, up to `ui.max_history_per_instance`, and the new question comes last. The built-in system prompt reads:

```text
You are an annotation assistant for the task "{task_name}".

Task description: {task_description}
Available labels: {annotation_labels}

The annotator is currently looking at this text:
---
{instance_text}
---

Help the annotator think through how to annotate this instance. You may:
- Highlight relevant aspects of the text
- Explain what to look for given the task description
- Clarify label definitions if asked

Do NOT tell the annotator which label to choose. Your role is to help them reason through the decision themselves.
```

Potato fills `{task_name}` from `annotation_task_name` and `{task_description}` from `annotation_task_description`. The label summary lists each scheme's name with the scheme's label names, or with the scheme's `description` for a scheme without labels, such as a text box. The item's text is the field that `item_properties.text_key` names. For an image task whose text field holds an image path or URL, the model receives the path or URL as text. Potato does not send the annotator's username, the labels the annotator has chosen, or any other item.

Potato does not check the model's replies. The built-in prompt asks the model not to choose a label, and a model can still name a label when an annotator asks for the answer directly. A comparison group set with `ai_support.holdout` measures how far the chat moves a study's labels, and [Chat support and AI support](#chat-support-and-ai-support) gives the setup.

## Chat logs in user_state.json

Potato writes the conversation to the annotator's state file, `<output_annotation_dir>/<username>/user_state.json`, after every reply. Each item's messages sit under `instance_id_to_behavioral_data`, in the item's `chat_history` list:

```json
{
  "instance_id_to_behavioral_data": {
    "sent_1": {
      "chat_history": [
        {
          "role": "user",
          "content": "Does the second clause outweigh the first?",
          "timestamp": 1764340000.0,
          "instance_id": "sent_1",
          "response_time_ms": null
        },
        {
          "role": "assistant",
          "content": "Compare how strongly each clause is worded ...",
          "timestamp": 1764340002.4,
          "instance_id": "sent_1",
          "response_time_ms": 2400
        }
      ]
    }
  }
}
```

Each record holds the role, the message text, a Unix timestamp, and the item ID. An assistant record also holds `response_time_ms`, the time the model took to reply. Every exchange adds a `chat_message_sent` event to the item's `interactions` list as well, with the length of the question, the length of the reply, and the response time. Potato keeps every message in `chat_history`, and `max_history_per_instance` trims only the conversation sent to the model.

Potato's export formats leave the chat history out. Read the state files directly to analyze the conversations:

```python
import json
from pathlib import Path

for state_file in Path("annotation_output").glob("*/user_state.json"):
    state = json.loads(state_file.read_text())
    user = state.get("user_id", state_file.parent.name)
    for item_id, record in state.get("instance_id_to_behavioral_data", {}).items():
        for message in record.get("chat_history", []):
            print(user, item_id, message["role"], message["content"][:80])
```

The script prints one line per message, with the annotator, the item ID, the role, and the first 80 characters of the message.

## Privacy in a study

A hosted endpoint sends the item's text, the task description, the label names, and every message the annotator types to the provider's servers. The hosted types are `openai`, `anthropic`, `gemini`, `huggingface`, `openrouter`, `openai_vision`, and `anthropic_vision`. A self-hosted model behind `ollama`, `ollama_vision`, `vllm`, or an OpenAI-compatible `base_url` keeps the conversation on the study's own servers. Choose a self-hosted model when the items cannot leave the study's machines, and check the provider's data-retention terms before a study sends items to a hosted model.

An annotator can type personal details into the chat box, and Potato stores the typed text verbatim in `user_state.json`. Treat the state files as identifiable study data when the sidebar is on. The export formats leave the chat history out, but a data archive from `/admin/api/data/archive` or `potato deploy pull` copies the whole output directory, `user_state.json` files included, so the chat logs travel with every archive. Tell annotators on the consent or instructions page that the chat goes to a model and that Potato records every message.

## Chat support and AI support

`chat_support` runs on an endpoint separate from `ai_support` and needs no `ai_support` block. A config can enable both blocks with different models. For example, a hosted model can write the [AI support](/docs/features/ai-support) hints while a local model answers in the sidebar.

`ai_support.holdout` withholds every kind of AI assistance from a random share of annotator-item pairs, and the chat sidebar is among the kinds withheld. Potato reads the holdout setting even when `ai_support.enabled` is false. On a held-out pair, the annotation page loads without the **Ask AI** button, and the server refuses to send chat messages about the item, with a 403. A blind request from the [labeling engine](/docs/labeling-engine/overview#requests-on-the-annotation-page) turns the sidebar off in the same way. See [A no-assistance comparison group](/docs/features/ai-support#a-no-assistance-comparison-group) for the `holdout` block and the admin dashboard's comparison of the two groups.

## Example project

Potato ships a runnable example in [`examples/ai-assisted/llm-chat`](https://github.com/davidjurgens/potato/tree/master/examples/ai-assisted/llm-chat). The example is a sentiment task with four labels and the sidebar on a local Ollama model, `llama3.2`. With Ollama running, pull the model and start the server from the example's folder:

```bash
ollama pull llama3.2
potato start config.yaml -p 8000
```

Log in at `http://localhost:8000`, click **Ask AI** in the top bar, and ask about the first review.

## Further reading

- [AI support](/docs/features/ai-support), for hints, keyword highlights, rationales, and the no-assistance comparison group
- [Visual AI support](/docs/features/visual-ai-support), for the vision endpoints the sidebar can share
- [Behavioral tracking](/docs/features/behavioral-tracking), for the other interaction data in `user_state.json`
- [IT handoff](/docs/deployment/it-handoff), for the questions an IT team asks about features that send items to a model provider

For implementation details, see the [source documentation](https://github.com/davidjurgens/potato/blob/master/docs/ai-intelligence/chat_support.md).
