# Fine-tuning

**Last updated:** 2026-11-21T09:07:48-03:00 (UTC-3)

Create and monitor fine-tuning jobs on the OpenAI API through the gateway, with files and jobs isolated per team. Flags: `fine_tuning` and `batches`. Interactive reference at `/docs/swagger/`, tag **Fine-tuning**.

## How to turn it on

1. Turn on the `fine_tuning` and `batches` flags. Both require storage.
2. Declare an alias with the `fine_tuning` capability. The first target must use the `openai` protocol:

   ```yaml
   models:
     - id: ft-mini
       capabilities: [chat, fine_tuning]
       targets:
         - {provider: openai, model: gpt-4o-mini-2024-07-18}
   ```

3. Give the key the `batches:write` scope, to upload files, and `route:fine_tuning`, plus the alias in its allowlist. The key must belong to a team.

## How to use it

```python
client = openai.OpenAI(api_key=KEY, base_url="https://gateway.example.com/v1")
f = client.files.create(file=open("train.jsonl", "rb"), purpose="fine-tune")
job = client.fine_tuning.jobs.create(model="ft-mini", training_file=f.id, metadata={"run": "nightly"})
client.fine_tuning.jobs.retrieve(job.id)
client.fine_tuning.jobs.list_events(job.id, limit=10)
client.fine_tuning.jobs.cancel(job.id)
```

## How it works

| Aspect | Behavior |
|---|---|
| **Target** | The alias's first target, with no fallback. The connection and the upstream model are recorded in the job |
| **Files** | `fine-tune` files from the key's team, as JSONL of objects. The gateway copies the file to the provider when the job is created and deletes the copy when the job finishes or is rejected |
| **Isolation** | A job from another team, a forged ID or a raw ID from the provider account return 404, without calling the provider |
| **Listing** | Only the team's jobs, paginated by the gateway (`after`, `limit` from 1 to 100, `has_more`). It never uses the provider account's list. Accepts a `metadata[k]=v` filter |
| **Object** | Shows the alias in `model` and the gateway's file IDs. Status, `fine_tuned_model` and `trained_tokens` come from the provider |
| **Errors** | A provider rejection arrives as it came (status and body) |
| **Rejected** | `integrations`, because the W&B integration belongs to the provider organization |
| **Usage** | Each call enters the ledger as `fine_tuning`, with no tokens. Training is billed by the provider, outside budget and TPM |
| **Console** | The **Fine-tuning** screen: jobs per team, with the last reported state and cancellation for the team admin |

## Using the trained model

When the job finishes, `fine_tuned_model` carries the ID at the provider (`ft:...`). To call it through the gateway, the operator creates an alias with that ID as the target model.

## Limits

- **OpenAI is shutting down self-serve fine-tuning.** New organizations can no longer create jobs since May 2026, and the general end is planned for January 2027; inference on already-trained models continues. For that reason, a real end-to-end job was not run.
- Providers with an OpenAI-compatible API follow the same path, but none has been validated. Azure OpenAI and Vertex are not supported yet.
- `result_files` carries provider file IDs, which the gateway's Files API does not read.
- Revoking the key does not cancel the job at the provider; cancel it explicitly.
- The file copies at the provider are deleted when a retrieve, list or cancel call observes that the job has ended.

## Admin API routes

Every route below needs a console session or an admin Bearer token, the role's permission and, for writes, the `X-CSRF-Token` header; the full contract is in the [API reference](/docs/api/).

| Route | What it does |
| --- | --- |
| `GET /api/v1/admin/fine-tuning/jobs` | Lists the fine-tuning jobs the gateway owns, with the last state the provider reported and the trained model |
| `POST /api/v1/admin/fine-tuning/jobs/{id}/cancel` | Cancels the job at its provider |

## See also

- [Batches and Files API](/docs/features/batches/).
- [Capabilities and routes](/docs/features/capabilities/).
