> ## 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 span correlation.

> [!WARNING]
>
> You are reading the docs of Galtea 5.11. [Read this page in the latest version](https://docs.galtea.ai/sdk/api/span/set-context.md).

## Returns

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

## Example

```python theme={"system"}
span_context = set_context(trace_id=trace_id)
```

## Parameters

**trace\_id** `string` *required*

The trace ID to associate spans with. All subsequent spans will be linked to this ID.

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

## How It Works

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

1. **Span Correlation** - All spans are linked to the same trace
2. **Automatic Export** - Spans are batched and exported to Galtea API


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.