# Retry a message > Throw away one of the agent's answers and write it again Retry regenerates an agent answer in a conversation you started with the Chat API. QwryAI finds the customer message the answer replied to, deletes that answer and everything after it, and answers the customer message again. Use it when an answer failed, was cut off, or you want a fresh one. The new answer gets a new id and uses message credits like any other. ```http POST https://api.qwryai.com/api/v2/agents/{agentId}/conversations/{conversationId}/retry ``` ## Authentication Send your API key as `Authorization: Bearer `. The key needs **Chat with agents**. See [Authentication](/docs/api-reference/v2/authentication). ## Path parameters | Name | Type | Required | Description | | - | - | - | - | | `agentId` | `string` | Yes | The agent's id, from `list_agents`. | | `conversationId` | `string` | Yes | The conversation's id, from the answer's `metadata.conversationId`. | ## Body parameters | Name | Type | Required | Description | | - | - | - | - | | `messageId` | `string` | Yes | The id of the agent answer to regenerate: `data.id` from a non-streamed answer, or `messageId` from a stream's `start` and `finish` events. | | `stream` | `boolean` | No | Stream the new answer as Server-Sent Events. Defaults to `true`. | Any other field is refused with `VALIDATION_INVALID_BODY`. > [!WARNING] > > Retrying deletes the answer you name and every message after it in the conversation, customer messages included. Retry the latest answer unless you mean to rewind the conversation. ## Request example > [tab] cURL > > ```bash > curl -X POST 'https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/conversations/fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3/retry' \ > -H 'Authorization: Bearer YOUR_API_KEY' \ > -H 'Content-Type: application/json' \ > -d '{ > "messageId": "5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10", > "stream": false > }' > ``` > [tab] JavaScript > > ```javascript > const conversationId = "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3"; > const response = await fetch( > `https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/conversations/${conversationId}/retry`, > { > method: "POST", > headers: { > Authorization: "Bearer YOUR_API_KEY", > "Content-Type": "application/json", > }, > body: JSON.stringify({ > messageId: "5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10", > stream: false, > }), > } > ); > const { data } = await response.json(); > ``` > [tab] Python > > ```python > import requests > > conversation_id = "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3" > response = requests.post( > f"https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/conversations/{conversation_id}/retry", > headers={"Authorization": "Bearer YOUR_API_KEY"}, > json={"messageId": "5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10", "stream": False}, > ) > data = response.json()["data"] > ``` ## Response The new answer, in the same shape as [Chat with an agent](/docs/api-reference/v2/agents/chat-with-an-agent). With streaming on, you get the same events as a chat stream. ```json { "data": { "id": "9e2d4b7c-61a3-4f0e-8c5d-2b7a9f1e3c48", "role": "assistant", "parts": [ { "type": "text", "text": "Order 1042 left our warehouse on Monday. UPS expects to deliver it by Friday." } ], "metadata": { "conversationId": "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3", "userMessageId": "0c4f6a2e-8d13-4b7a-b0e9-1a2c3d4e5f60", "userId": "customer_1042", "finishReason": "stop", "usage": { "credits": 1 } } } } ``` `userMessageId` is the customer message that was answered again. `data.id` is the new answer's id; the old one no longer exists. ## Errors | HTTP status | Code | When | | - | - | - | | 400 | `VALIDATION_INVALID_BODY` | No `messageId`, or an unknown field. | | 400 | `CHAT_RETRY_NO_USER_MESSAGE` | No customer message comes before that answer. | | 401 | `AUTH_MISSING_API_KEY` / `AUTH_INVALID_API_KEY` | No key, or an unknown, deleted or expired key. | | 402 | `CHAT_CREDITS_EXHAUSTED` | Not enough message credits for a new answer. | | 403 | `SUBSCRIPTION_API_RESTRICTED_PLAN` | The workspace's plan doesn't include API access. | | 403 | `CHAT_CONVERSATION_MISMATCH` | The conversation doesn't exist or belongs to another agent. | | 404 | `AGENT_NOT_FOUND` | No agent with that id in your workspace. | | 404 | `CHAT_RETRY_MESSAGE_NOT_FOUND` | `messageId` isn't an agent answer in this conversation. | | 409 | `CHAT_CONVERSATION_NOT_ONGOING` | The conversation ended or was taken over, another answer is still being written, or a [client action](/docs/api-reference/v2/client-actions) is waiting for its result. | | 429 | `RATE_LIMIT_TOO_MANY_REQUESTS` | Too many requests or open streams. Wait for `Retry-After` seconds. | See [Error Handling](/docs/api-reference/v2/error-handling) for every code.