# use_environment

> **Function** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/config/use_environment)

Bind an immutable environment snapshot for the duration of the block.

The binding is a `ContextVar`, so it is copied into `asyncio.to_thread`
workers, and it ends when the block exits. Anything that runs later -- a
lazy model switch, a summary, a compaction rebuild -- is outside it, which
is why those middlewares carry an `environ` field and re-enter this
contextmanager at request time instead of relying on the ambient binding.

`None` binds nothing, and `active_environment` then falls back to
`os.environ`.

## Signature

```python
use_environment(
    environ: Mapping[str, str] | None,
) -> Iterator[None]
```

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/4bfa52000ef65ac623d8ada0cb67f67cbdd663ea/libs/code/deepagents_code/config.py#L520)