> ## 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

<div className="flex items-center gap-2">
  <Badge color="teal">Function</Badge>
</div>

> This function is defined in the [**context\_events**](../modules/context_events) 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().

## Signature

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

## Parameters

<ParamField query="func" type="Any" required={true}>
  The callable to wrap.
</ParamField>

### Returns

<ResponseField name="Returns" type="Any">
  A wrapped callable that copies the current context.
</ResponseField>

## 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 title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-agents/praisonaiagents/trace/context_events.py#L117">
  `praisonaiagents/trace/context_events.py` at line 117
</Card>

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Context Concept" icon="layer-group" href="/docs/docs/concepts/context" />

  <Card title="Context Management" icon="sliders" href="/docs/docs/features/context-management" />

  <Card title="Context Strategies" icon="diagram-project" href="/docs/docs/features/context-strategies" />
</CardGroup>
