> For the complete documentation index, see [llms.txt](https://sonic-ai-works.gitbook.io/sonic-networks-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sonic-ai-works.gitbook.io/sonic-networks-docs/api-reference/sonic-api-reference/music-generation-and-operations.md).

# Music Generation & Operations

Provider-independent music generation, durable GenerationOperation identity, provider attempts, callbacks, asset ingestion and exactly-once credit settlement.

**Status:** `Canonical / v1.3`

The SONIC public music API is provider-independent:

```http
POST /api/v1/music/generate
```

Clients do not call a Kie-specific generation route for normal product traffic.

### Generate music

```http
POST /api/v1/music/generate
Authorization: Bearer sk_sonic_live_...
Idempotency-Key: music_8831
Content-Type: application/json
```

```json
{
  "prompt": "Cinematic ambient electronic track",
  "instrumental": true,
  "model": "suno-v5.5",
  "duration": 120
}
```

`duration` is only forwarded when the selected provider/model contract supports it. For the Kie Suno adapter, `suno-v5.5` maps to `V5_5`.

Accepted response:

```json
{
  "data": {
    "operationId": "op_01K...",
    "status": "queued",
    "billing": {
      "mode": "SONIC_MANAGED",
      "creditsReserved": "12"
    }
  },
  "meta": {
    "requestId": "req_01K..."
  }
}
```

Provider task IDs are intentionally absent.

### Durable identity

```
GenerationOperation op_01K...
    │
    ├── ProviderAttempt #1 → KIE → providerTaskId A
    └── ProviderAttempt #2 → KIE/SUNO_DIRECT → providerTaskId B
```

One customer operation can own multiple provider attempts. Retries/failover never replace the durable SONIC operation ID.

### Lifecycle

```
queued
→ dispatched
→ processing
→ completed
```

Terminal alternatives are `failed`, `cancelled` and `expired`. Status and stage remain separate. Kie staged callbacks normalize into SONIC stages such as `text`, `first`, `provider_complete`, `ingesting` and `finished`.

Terminal state is monotonic: stale callbacks or recovery polls cannot regress a completed operation.

### Operation endpoints

```http
GET  /api/v1/operations/{operationId}
GET  /api/v1/operations/{operationId}/result
POST /api/v1/operations/{operationId}/cancel
```

Cancellation is idempotent and cannot retroactively undo provider work already completed.

### Dispatch sequence

```
API gateway
→ authenticate + scope + limits
→ idempotency recovery/create
→ GenerationOperation
→ quote + reserve SONIC Credits
→ provider router
→ ProviderAttempt
→ provider create
→ provider callback / recovery poll
→ normalize
→ ingest durable assets
→ settle/release exactly once
→ provider-cost reconciliation
```

### Provider selection

Standard requests let SONIC choose the provider route. An explicit policy such as:

```json
{
  "providerPolicy": {
    "preferred": "kie",
    "fallback": ["suno-direct"]
  }
}
```

requires the `providers:select` scope.

### Idempotency

Music generation requires `Idempotency-Key`. The domain includes tenant, workspace, endpoint, key and canonical request hash.

Same key + same payload recovers the existing operation. Same key + different payload returns `409 IDEMPOTENCY_CONFLICT`.

### Callback + polling convergence

```
provider callback ─┐
                   ├─► normalizeProviderState()
recovery polling ──┘
                           ↓
                  monotonic merge
                           ↓
                 GenerationOperation
```

Callbacks are primary. Centralized provider polling exists only for recovery and is bounded by provider task query limits.

### Asset durability

Completed provider media is ingested immediately into SONIC-managed storage. A provider URL is never treated as a permanent project asset.

### Accounting

Customer billing and provider cost are independent ledgers:

```
SONIC AI Credits → customer price
Kie/Suno credits → provider cost signal
```

Every customer charge uses reserve → settle/release exactly once. Provider cost reconciliation never directly subtracts `creditsConsumed` from the user's SONIC AI Credit balance.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sonic-ai-works.gitbook.io/sonic-networks-docs/api-reference/sonic-api-reference/music-generation-and-operations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
