---
title: "Custom forward-backward pass"
method: POST
path: "/v1/rl/training-sessions/{session_id}/operations/custom-forward-backward"
tags: ["RL"]
---

# Custom forward-backward pass

`POST /v1/rl/training-sessions/{session_id}/operations/custom-forward-backward`

## Request

- Base URL: `https://api.together.ai`
- URL: `https://api.together.ai/v1/rl/training-sessions/{session_id}/operations/custom-forward-backward`
- Auth: HTTP bearer

Submits a forward-backward pass driven by externally computed gradients of the loss with respect to per-token log-probabilities.

## Path parameters

- `session_id` string, required: Training session ID

## Headers

- `Idempotency-Key` string, required: Required key that makes retries return the original operation; use a new key for changed request bodies.

## Request body

- RLCustomForwardBackwardBody: Request body for a custom forward-backward pass.
  - `samples` RLTrainingSample[], required: Batch of training samples
    - `model_input` RLModelInput, required
      - `chunks` RLModelInputChunk[], required: Input chunks for the model
        - `encoded_text` RLEncodedTextChunk, required: Pre-tokenized text content for a model input chunk.
          - `tokens` union[], required: Pre-tokenized text input
            - union
              - …
    - `loss_fn_inputs` object, required: Per-token loss tensors keyed by name. Include `target_tokens` and the inputs required by the selected loss. Each tensor must declare `int64` or `float32`, be one-dimensional, and have the same length.
    - `routed_experts_key` string: Opaque key returned with a sampled sequence. Pass it unchanged with the corresponding training sample to reuse the same expert selections. The selections must cover the entire training sample or all but its final token. Training fails if the key is no longer available.
  - `gradients` RLTargetLogprobGradients[], required: Per-sample per-token gradients of the loss with respect to log-probabilities
    - `data` number[], required: Float array of per-token gradients (d loss / d log p)
    - `dtype` 'D_TYPE_UNSPECIFIED' | 'D_TYPE_INT64' | 'D_TYPE_FLOAT32' | 'D_TYPE_BFLOAT16'

## Response `200`

- RLCustomForwardBackwardOperation: Async custom forward-backward pass operation
  - `id` string, required: Operation ID
  - `status` 'TRAINING_OPERATION_STATUS_UNSPECIFIED' | 'TRAINING_OPERATION_STATUS_PENDING' | 'TRAINING_OPERATION_STATUS_RUNNING' | 'TRAINING_OPERATION_STATUS_COMPLETED' | 'TRAINING_OPERATION_STATUS_FAILED', required
  - `output` RLCustomForwardBackwardResult: Result of a custom forward-backward pass operation
  - `error` RLTrainingOperationError: Error details for a failed training operation
    - `code` 'TRAINING_OPERATION_ERROR_CODE_UNSPECIFIED' | 'TRAINING_OPERATION_ERROR_CODE_RESOURCE_EXHAUSTED' | 'TRAINING_OPERATION_ERROR_CODE_TIMEOUT' | 'TRAINING_OPERATION_ERROR_CODE_INTERNAL_ERROR' | 'TRAINING_OPERATION_ERROR_CODE_SESSION_NOT_ACTIVE' | 'TRAINING_OPERATION_ERROR_CODE_INVALID_INPUT' | 'TRAINING_OPERATION_ERROR_CODE_NON_FINITE_LOSS': Application error code for a failed training operation
    - `message` string: Human-readable error message

## Other responses

- `default`: An unexpected error response.

## Changes

- **2026-10-22** `bb5257b3173a`: 1 info
  - endpoint added

[Operation history](https://skmtc.dev/together/apis/together-apis/changes/v1/rl/training-sessions/:session_id/operations/custom-forward-backward/post.md)

---

[API](https://skmtc.dev/together/apis/together-apis.md) - [All operations](https://skmtc.dev/together/apis/together-apis/llms.txt) - [OpenAPI document](https://skmtc.dev/together/apis/together-apis/revisions/93438ddac711?raw)
