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

# Copy Context To Callable • AI Agent SDK

> copy_context_to_callable: Wrap a callable to copy the current context to the new thread/executor.

# copy\_context\_to\_callable

Function

> This function is defined in the [**context\_events**](https://praison.ai/docs/sdk/reference/praisonaiagents/modules/context_events.md) module.

Wrap a callable to copy the current context to the new thread/executor.

This is needed because contextvars are NOT automatically propagated to
thread pool executors. Use this when calling run\_in\_executor().

The whole context is copied, so the replay state (emitter, run id, branch
id and the open-span stack that yields `parent_seq`) reaches the thread.

## Signature

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
def copy_context_to_callable(func: Any) -> Any
```

## Parameters

**func** `Any` *required*

The callable to wrap.

### Returns

**Returns** `Any`

A wrapped callable that copies the current context.

## Usage

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Instead of:
    await loop.run_in_executor(None, lambda: agent.chat(prompt))

    # Use:
    await loop.run_in_executor(None, copy_context_to_callable(lambda: agent.chat(prompt)))
```

## Uses

* `contextvars.copy_context`
* `ctx.run`

## Used By

* [`AgentTeam.arun_all_tasks`](../functions/AgentTeam-arun_all_tasks)

## Source

> [card] [View on GitHub](https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-agents/praisonaiagents/trace/context_events.py#L266)
>
> `praisonaiagents/trace/context_events.py` at line 266

***

## Related Documentation

> [card] [Context Concept](https://praison.ai/docs/docs/concepts/context)

> [card] [Context Management](https://praison.ai/docs/docs/features/context-management)

> [card] [Context Strategies](https://praison.ai/docs/docs/features/context-strategies)


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