> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cyborg.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Train Index

Builds the index using the specified training configuration. Required before efficient querying.
Prior to calling this, all queries will be conducted using encrypted exhaustive search.
After, they will be conducted using encrypted ANN search.

```cpp theme={null}
void TrainIndex(const TrainingConfig& training_config, const KeyContext& key);
```

### Parameters

| Parameter         | Type                                        | Description                                                                                                      |
| ----------------- | ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| `training_config` | [`TrainingConfig`](../types#trainingconfig) | Training parameters including n\_lists, batch size, max iterations, tolerance, and max memory.                   |
| `key`             | [`KeyContext`](../types#keycontext)         | Key context for the operation. A bare 32-byte index key (the `index_key`) implicitly converts to a `KeyContext`. |

<Tip>There must be at least `2 * n_lists` or `10,000` (whichever is greater) vector embeddings in the index prior to calling this function. The `n_lists` parameter is configured within the `TrainingConfig` object.</Tip>

### Exceptions

<AccordionGroup>
  <Accordion title="std::runtime_error">
    * Throws if there are not enough vector embeddings in the index for training (must be at least `2 * n_lists`).
    * Throws if the index could not be trained.
  </Accordion>
</AccordionGroup>

### Example Usage

```cpp theme={null}
// TrainingConfig(n_lists, batch_size, max_iters, tolerance, max_memory)
// batch_size of 0 (auto) lets CyborgDB choose the batch size.
cyborg::TrainingConfig config(1024, 0, 100, 1e-6, 0);
index->TrainIndex(config, index_key);
```

<Tip>`index_key` is the 32-byte `std::array<uint8_t, 32>` index KEK and converts implicitly to a `KeyContext`.</Tip>
