---
source: https://iinventedlight.com/library/notes/python-contextvars.md
title: "contextvars stores context-local state for async tasks"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); Public domain or CC0-1.0 (Python PEPs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, contextvars — Context Variables, PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE); Python PEPs, PEP 567 – Context Variables, Public domain or CC0-1.0 (https://peps.python.org/pep-0567/ (Copyright section: public domain))."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: contextvars"
tags: [python, contextvars, asyncio, pep-567]
client: Librarian
sources:
  - id: S1
    title: "contextvars — Context Variables"
    publisher: Python docs
    url: https://docs.python.org/3/library/contextvars.html
    accessed: 2026-11-16
    license: "PSF-2.0"
  - id: S2
    title: "PEP 567 – Context Variables"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0567/
    accessed: 2026-11-16
    license: "Public domain or CC0-1.0"
---
# contextvars stores context-local state for async tasks
**Answer:** The `contextvars` module provides APIs to manage, store, and access context-local state. Declare a variable with `ContextVar`. Use `copy_context` and `Context` in asynchronous frameworks. Context managers that have state should use context variables instead of `threading.local` so that state does not leak across concurrent work. Context Variables should be created at the top module level, and never in closures.

PEP 567 introduced the module for Python 3.7. It is similar to thread-local storage, but it also keeps values per asynchronous task such as `asyncio.Task`.

## See also

- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)
- [python-contextlib](https://iinventedlight.com/library/notes/python-contextlib/)
- [python-asyncio-awaitables](https://iinventedlight.com/library/notes/python-asyncio-awaitables/)

## Claims

- id: C1
  claim: contextvars manages context-local state.
  source: S1
  evidence: "This module provides APIs to manage, store, and access context-local"

- id: C2
  claim: Context variables should be created at module level.
  source: S1
  evidence: "Context Variables should be created at the top module"

- id: C3
  claim: PEP 567 proposed the contextvars module.
  source: S2
  evidence: "This PEP proposes a new ``contextvars`` module and a set of new"

- id: C4
  claim: The design also tracks values per asynchronous task.
  source: S2
  evidence: "it also allows correctly keeping track of values per asynchronous task"

## Sources

- [S1] contextvars — Context Variables. Python docs. https://docs.python.org/3/library/contextvars.html . License: PSF-2.0. Accessed 2026-11-16.

- [S2] PEP 567 – Context Variables. Python PEPs. https://peps.python.org/pep-0567/ . License: Public domain or CC0-1.0. Accessed 2026-11-16.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, contextvars — Context Variables, PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE); Python PEPs, PEP 567 – Context Variables, Public domain or CC0-1.0 (https://peps.python.org/pep-0567/ (Copyright section: public domain)). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
