# Train agent > Train every new or changed source, as Retrain agent does on the Data sources page Adding or editing a source doesn't retrain the agent by itself. `train_pending_sources` is the API's **Retrain agent** button: it trains every source with untrained changes (new, changed or failed) and removes every source marked for removal, in one review. To train a single source instead, use `start_training` with its `sourceId` and `expectedVersion`. Training runs in the background. Applying the review queues it; follow progress with `check_training_status` or `find_sources_by_status`. See [Sources](/docs/api-reference/v2/sources). ```http POST https://api.qwryai.com/public/v1/tools/train_pending_sources ``` ## Authentication Send your API key as `Authorization: Bearer `, or in an `x-api-key` header. The key needs **Change sources and training**. See [Authentication](/docs/api-reference/v2/authentication). ## Body parameters | Name | Type | Required | Description | | - | - | - | - | | `chatbotId` | `string` | Yes | The agent's id, from [List agents](/docs/api-reference/v2/agents/list-agents). | `start_training` takes `chatbotId`, `sourceId` and `expectedVersion` (the source's `version` from `list_sources`) to train that one source. ## Request example > [tab] cURL > > ```bash > curl -X POST 'https://api.qwryai.com/public/v1/tools/train_pending_sources' \ > -H 'Authorization: Bearer YOUR_API_KEY' \ > -H 'Content-Type: application/json' \ > -d '{"chatbotId": "YOUR_AGENT_ID"}' > > # Apply the review it returns to queue training > curl -X POST 'https://api.qwryai.com/public/v1/reviews/REVIEW_ID/apply' \ > -H 'Authorization: Bearer YOUR_API_KEY' > ``` > [tab] JavaScript > > ```javascript > const API = "https://api.qwryai.com/public/v1"; > const headers = { Authorization: "Bearer YOUR_API_KEY", "Content-Type": "application/json" }; > > const { data: review } = await fetch(`${API}/tools/train_pending_sources`, { > method: "POST", headers, body: JSON.stringify({ chatbotId: "YOUR_AGENT_ID" }), > }).then((r) => r.json()); > > const { data: result } = await fetch(`${API}/reviews/${review.reviewId}/apply`, { > method: "POST", headers, > }).then((r) => r.json()); > console.log(result.data); > ``` > [tab] Python > > ```python > import requests > > API = "https://api.qwryai.com/public/v1" > headers = {"Authorization": "Bearer YOUR_API_KEY"} > > review = requests.post(f"{API}/tools/train_pending_sources", headers=headers, > json={"chatbotId": "YOUR_AGENT_ID"}).json()["data"] > result = requests.post(f"{API}/reviews/{review['reviewId']}/apply", headers=headers).json()["data"] > print(result["data"]) > ``` ## Response The review lists each source that will train: ```json { "success": true, "data": { "status": "review_required", "reviewId": "6f0a3d92-7c1e-4b58-a2d4-9e3b5c1f7a80", "tool": "train_pending_sources", "changes": [ { "title": "Train source: Returns policy", "before": { "training": "Changed" }, "after": { "training": "Queued" } }, { "title": "Train source: Shipping FAQ", "before": { "training": "Not trained" }, "after": { "training": "Queued" } } ], "unchanged": [], "expiresAt": "2026-12-01T10:15:00.000Z", "next": "Show these changes to the user. Call apply_change with this reviewId only after they approve; the review expires in 15 minutes." } } ``` Applying it queues a training job for each source: ```json { "success": true, "data": { "status": "succeeded", "reviewId": "6f0a3d92-7c1e-4b58-a2d4-9e3b5c1f7a80", "tool": "train_pending_sources", "data": { "applied": true, "trained": [ { "jobId": "JOB_ID_1", "sourceId": "SOURCE_ID_1", "status": "pending", "applied": true, "training": "pending" }, { "jobId": "JOB_ID_2", "sourceId": "SOURCE_ID_2", "status": "pending", "applied": true, "training": "pending" } ] }, "receipt": { "applied": true, "queued": 2, "notQueued": [], "coverage": "Admission is committed for each queued source. Job status describes asynchronous training; admission alone does not prove completion." } } } ``` > [!NOTE] > > One review trains up to 20 sources. If more are waiting, call the tool again after the first batch is queued. Sources that are already training are skipped. ## Errors | HTTP status | Code | When | | - | - | - | | 400 | `INVALID_ARGUMENTS` | No source is waiting to be trained, or the body has another field. | | 400 | `CHATBOT_ID_REQUIRED` | The body has no valid `chatbotId`. | | 401 | `API_KEY_REQUIRED` / `INVALID_API_KEY` | No credential, or an unknown, deleted or expired one. | | 403 | `INSUFFICIENT_PERMISSION` | The key doesn't hold **Change sources and training**. | | 404 | `CHATBOT_NOT_FOUND` | No agent with that `chatbotId` in your workspace. | | 409 | `STALE_REVIEW` / `REVIEW_EXPIRED` | Applying: the sources changed after the review, or 15 minutes passed. Call the tool again. | | 429 | `INTEGRATION_RATE_LIMIT_EXCEEDED` | More than 2,000 requests in 15 minutes. | See [Error Handling](/docs/api-reference/v2/error-handling) for every code.