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

# Set Context

> Set Galtea context values for trace correlation.

## Returns

Returns a context token that must be passed to `clear_context()` for cleanup.

## Example

```python theme={"system"}
trace_context = set_context(inference_result_id=inference_result_id)
```

## Parameters

<ResponseField name="inference_result_id" type="string" required>
  The inference result ID to associate traces with. All subsequent traces will be linked to this ID.
</ResponseField>

<Tip>
  **Best Practice:** Always use `set_context()` with a `try/finally` block to ensure `clear_context()` is called even if an exception occurs.
</Tip>

## How It Works

When you call `set_context()`, it attaches the provided ID to the current OpenTelemetry context. Any traces created by `@trace` decorated functions or `start_trace()` will automatically include this ID as an attribute, enabling:

1. **Trace Correlation** - All traces are linked to the same inference result
2. **Automatic Export** - Traces are batched and exported to Galtea API
