# Chat with an agent

Ask an agent a question in chat and read its reply. Each [conversation](/docs/concepts/conversations.md) keeps its history, so the agent sees what you said before.

## Before you start

You need an agent whose instructions include "Refunds take 5 business days." See [Create an agent](/docs/agents/create-agent.md). From your own app, you also need an API key. See [Create an API key](/docs/authentication/api-keys.md).

## Chat with an agent

**Studio**

1. In your project, open **Chat**.
2. Pick your agent under the message box.
3. Write "How long does a refund take?" in the message box.
4. Press Enter. You should now see the reply say refunds take 5 business days.

**API**

1. Set `PROJECT` to your project's ID and `AGENT_ID` to your agent's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)):

```bash
export PROJECT=<id>
export AGENT_ID=<agent id>
```

2. Create the conversation with the first message:

```bash
curl -X POST https://api.veryfront.com/conversations \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "project_reference": "'$PROJECT'",
    "type": "chat",
    "title": "Refund question",
    "initial_message": {
      "role": "user",
      "parts": [{ "type": "text", "text": "How long does a refund take?" }]
    }
  }'
```

3. Set three values from the response: `CONVERSATION_ID` to its `id`, `MESSAGE_ID` to its `initial_message_id` and `PROJECT_ID` to its `project_id`:

```bash
export CONVERSATION_ID=<id>
export MESSAGE_ID=<initial_message_id>
export PROJECT_ID=<project_id>
```

4. Ask the agent to answer. The `Idempotency-Key` makes a retry return the same run:

```bash
curl -X POST https://api.veryfront.com/runs \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: refund-question-1" \
  -d '{
    "project_id": "'$PROJECT_ID'",
    "conversation_id": "'$CONVERSATION_ID'",
    "target": { "type": "agent", "id": "'$AGENT_ID'" },
    "input": {
      "agent_id": "'$AGENT_ID'",
      "messages": [{ "id": "'$MESSAGE_ID'", "role": "user", "parts": [{ "type": "text", "text": "How long does a refund take?" }] }],
      "tools": [],
      "context": []
    },
    "config": { "agent_admission": { "mode": "project", "input_message_id": "'$MESSAGE_ID'" } },
    "execution": { "runtime": { "type": "main_branch" } }
  }'
```

5. Set `RUN_ID` to the `id` in the response: `export RUN_ID=<id>`.
6. Read `GET /runs/$RUN_ID` until its `status` is `completed`, then read the messages. You should now see an `assistant` message with the agent's reply:

```bash
curl https://api.veryfront.com/conversations/$CONVERSATION_ID/messages \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Before you start: an agent | curl -X POST https://api.veryfront.com/projects/$VERYFRONT_PROJECT_ID/agents -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"id": "support", "name": "Support assistant", "system_prompt": "Answer customer questions about orders and refunds. Refunds take 5 business days. Keep replies to two sentences."}'
step: Set PROJECT and AGENT_ID | export PROJECT=$VERYFRONT_PROJECT_ID AGENT_ID=support
step: Create the conversation | curl -X POST https://api.veryfront.com/conversations -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"project_reference": "'$PROJECT'", "type": "chat", "title": "Refund question", "initial_message": {"role": "user", "parts": [{"type": "text", "text": "How long does a refund take?"}]}}' | tee conversation.json
step: Set CONVERSATION_ID, MESSAGE_ID and PROJECT_ID | export CONVERSATION_ID=$(jq -r .id conversation.json) MESSAGE_ID=$(jq -r .initial_message_id conversation.json) PROJECT_ID=$(jq -r .project_id conversation.json)
step: Ask the agent to answer | curl -X POST https://api.veryfront.com/runs -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -H "Idempotency-Key: refund-question-1" -d '{"project_id": "'$PROJECT_ID'", "conversation_id": "'$CONVERSATION_ID'", "target": {"type": "agent", "id": "'$AGENT_ID'"}, "input": {"agent_id": "'$AGENT_ID'", "messages": [{"id": "'$MESSAGE_ID'", "role": "user", "parts": [{"type": "text", "text": "How long does a refund take?"}]}], "tools": [], "context": []}, "config": {"agent_admission": {"mode": "project", "input_message_id": "'$MESSAGE_ID'"}}, "execution": {"runtime": {"type": "main_branch"}}}' | tee run.json
step: Set RUN_ID | export RUN_ID=$(jq -r .id run.json)
step: Wait until it is completed | until curl -s https://api.veryfront.com/runs/$RUN_ID -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r .status | grep -qE 'completed|failed'; do sleep 2; done
check: curl https://api.veryfront.com/conversations/$CONVERSATION_ID/messages -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: 5 business days
```

## With MCP

`my-project` is your project's reference: its slug or its ID. Your agent here is `support`; `veryfront__list_agents` lists them.

1. Call `veryfront__create_conversation`:

```json
{
  "project_reference": "my-project",
  "type": "chat",
  "title": "Refund question",
  "initial_message": {
    "parts": [{ "type": "text", "text": "How long does a refund take?" }]
  }
}
```

2. Call `veryfront__create_run`. Take `project_id`, `id` (as `conversation_id`) and `initial_message_id` from the conversation:

```json
{
  "input": {
    "project_id": "<project_id>",
    "conversation_id": "<id>",
    "target": { "type": "agent", "id": "support" },
    "input": {
      "agent_id": "support",
      "messages": [{ "id": "<initial_message_id>", "role": "user", "parts": [{ "type": "text", "text": "How long does a refund take?" }] }],
      "tools": [],
      "context": []
    },
    "config": { "agent_admission": { "mode": "project", "input_message_id": "<initial_message_id>" } },
    "execution": { "runtime": { "type": "main_branch" } }
  }
}
```

3. Call `veryfront__get_run` with the run's `id` as `run_id` until its `status` is `completed`.
4. Call `veryfront__list_conversation_messages` with the conversation's `id` as `conversation_id`. It worked when an `assistant` message holds the agent's reply.

## Next steps

* [Add a tool](/docs/agents/tools.md)
* [Debug a run](/docs/observability/debug.md)
