This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-py313-free-threaded-memory-boundary-standard

FastAPI Python 3.13 Free-Threaded Memory Boundary & State Encapsulation Standard

Mandates strict thread-safe memory boundaries and state encapsulation across FastAPI request lifecycles under Python 3.13 free-threaded (PEP 703) runtimes. Enforces total isolation of request contexts, thread-safe synchronization for shared state, and elimination of implicit GIL-dependent mutable patterns in dependency injection.

Prohibited Anti-Pattern: Unsynchronized Module-Level State and Shared Mutable Dependencies Under Python 3.13 Free-Threading

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Python 3.13 Free-Threaded Memory Boundary & State Encapsulation Standard - Mandates strict thread-safe memory boundaries and state encapsulation across FastAPI request lifecycles under Python 3.13 free-threaded (PEP 703) runtimes. Enforces total isolation of request contexts, thread-safe synchronization for shared state, and elimination of implicit GIL-dependent mutable patterns in dependency injection.
globs: app/**/*.py, src/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-py313-free-threaded-memory-boundary-standard"
---

# FastAPI Python 3.13 Free-Threaded Memory Boundary & State Encapsulation Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Module-Level State and Shared Mutable Dependencies Under Python 3.13 Free-Threading

## System Constraints
You are a Principal Software Architect enforcing the Python 3.13 Free-Threaded Concurrency Memory Boundary Standard in FastAPI applications. In Python 3.13t (free-threaded mode), the Global Interpreter Lock (GIL) is disabled, meaning operations previously treated as atomic due to GIL scheduling (dict updates, list modifications, in-place counters) will trigger catastrophic data races and memory corruption across concurrent worker threads. You must mandate strict state isolation via contextvars, request-scoped dependencies, and explicit mutual exclusion locks for all shared mutable memory structures.

## Strict Guidelines
- Engineers MUST ALWAYS isolate per-request contextual state using `contextvars.ContextVar` or strictly request-scoped FastAPI dependencies, never module-level mutable variables.
- Engineers MUST NOT rely on implicit GIL atomicity for any standard library collections (`dict`, `list`, `set`) or compound assignments (`+=`, `.append()`, `.setdefault()`) in free-threaded runtimes.
- Services MUST encapsulate all shared, cross-request in-memory state inside synchronization wrappers guarded by `threading.Lock`, `threading.RLock`, or lock-free concurrent primitives.
- Engineers MUST ALWAYS design FastAPI `Depends()` providers to return new instances or read-only immutable value objects (`dataclasses(frozen=True)` or Pydantic models) unless thread synchronization is explicitly implemented.
- Components NEVER expose shared mutable caches or state singletons through dependency injection without explicit, re-entrant concurrency controls.

## Bad vs Good Implementations
### Module-Level Mutable State vs Thread-Safe Encapsulation in Python 3.13t

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Python 3.13 free-threaded builds without the GIL, concurrent dict lookups/writes and list appends across threads are non-atomic. The module-level dictionary and the singleton dependency instance create race conditions, leading to torn data, lost updates, and interpreter crashes.
```python
from fastapi import FastAPI, Depends

app = FastAPI()

# CRITICAL BUG: Module-level mutable state accessed concurrently across free threads
active_metrics: dict[str, int] = {}

class UnsafeTracker:
    def __init__(self):
        self.events: list[str] = []

# Singleton instance injected into every concurrent request
singleton_tracker = UnsafeTracker()

def get_tracker() -> UnsafeTracker:
    return singleton_tracker

@app.post("/items/{item_id}")
async def process_item(item_id: str, tracker: UnsafeTracker = Depends(get_tracker)):
    # In Python 3.13 free-threaded mode, this creates torn reads/writes without the GIL
    active_metrics[item_id] = active_metrics.get(item_id, 0) + 1
    tracker.events.append(item_id)  # Data race: simultaneous list resizing causes memory corruption
    return {"id": item_id, "count": active_metrics[item_id]}
```

#### Production Standard (Correct)
> **Standard**: Shared state is fully protected behind a deterministic `threading.Lock` within `SynchronizedMetricsRegistry`, safeguarding dictionary mutations under Python 3.13 free-threaded execution. Request tracking is isolated to unique per-request allocations instantiated via FastAPI dependencies.
```python
import threading
from dataclasses import dataclass, field
from contextvars import ContextVar
from fastapi import FastAPI, Depends

app = FastAPI()

# Request-scoped isolation: Safe across OS threads and async tasks
request_id_ctx: ContextVar[str] = ContextVar("request_id_ctx", default="")

class SynchronizedMetricsRegistry:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._counts: dict[str, int] = {}

    def record_hit(self, key: str) -> int:
        with self._lock:
            updated = self._counts.get(key, 0) + 1
            self._counts[key] = updated
            return updated

@dataclass(slots=True)
class RequestScopedTracker:
    events: list[str] = field(default_factory=list)

# Safe thread-synchronized global state
metrics_registry = SynchronizedMetricsRegistry()

def get_request_tracker() -> RequestScopedTracker:
    # Guaranteed request-isolated allocation: no shared mutable state
    return RequestScopedTracker()

@app.post("/items/{item_id}")
async def process_item(
    item_id: str,
    tracker: RequestScopedTracker = Depends(get_request_tracker),
) -> dict[str, str | int]:
    count = metrics_registry.record_hit(item_id)
    tracker.events.append(item_id)
    return {"id": item_id, "count": count}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI Python 3.13 Free-Threaded Memory Boundary & State Encapsulation Standard

FastAPI Python 3.13 Free-Threaded Memory Boundary & State Encapsulation Standard defines an industrial-grade behavioral standard for backend engineering. Mandates strict thread-safe memory boundaries and state encapsulation across FastAPI request lifecycles under Python 3.13 free-threaded (PEP 703) runtimes. Enforces total isolation of request contexts, thread-safe synchronization for shared state, and elimination of implicit GIL-dependent mutable patterns in dependency injection. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unsynchronized Module-Level State and Shared Mutable Dependencies Under Python 3.13 Free-Threading Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnsynchronized Module-Level State and Shared Mutable Dependencies Under Python 3.13 Free-ThreadingCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS isolate per-request contextual state using `contextvars.ContextVar` or strictly request-scoped FastAPI dependencies, never module-level mutable variables.

Engineers MUST ALWAYS isolate per-request contextual state using `contextvars.ContextVar` or strictly request-scoped FastAPI dependencies, never module-level mutable variables.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT rely on implicit GIL atomicity for any standard library collections (`dict`, `list`, `set`) or compound assignments (`+=`, `.append()`, `.setdefault()`) in free-threaded runtimes.

Engineers MUST NOT rely on implicit GIL atomicity for any standard library collections (`dict`, `list`, `set`) or compound assignments (`+=`, `.append()`, `.setdefault()`) in free-threaded runtimes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Services MUST encapsulate all shared, cross-request in-memory state inside synchronization wrappers guarded by `threading.Lock`, `threading.RLock`, or lock-free concurrent primitives.

Services MUST encapsulate all shared, cross-request in-memory state inside synchronization wrappers guarded by `threading.Lock`, `threading.RLock`, or lock-free concurrent primitives.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Engineers MUST ALWAYS design FastAPI `Depends()` providers to return new instances or read-only immutable value objects (`dataclasses(frozen=True)` or Pydantic models) unless thread synchronization is explicitly implemented.

Engineers MUST ALWAYS design FastAPI `Depends()` providers to return new instances or read-only immutable value objects (`dataclasses(frozen=True)` or Pydantic models) unless thread synchronization is explicitly implemented.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER expose shared mutable caches or state singletons through dependency injection without explicit, re-entrant concurrency controls.

Components NEVER expose shared mutable caches or state singletons through dependency injection without explicit, re-entrant concurrency controls.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is FastAPI Python 3.13 Free-Threaded Memory Boundary & State Encapsulation Standard critical for AI coding assistants?

Mandates strict thread-safe memory boundaries and state encapsulation across FastAPI request lifecycles under Python 3.13 free-threaded (PEP 703) runtimes. Enforces total isolation of request contexts, thread-safe synchronization for shared state, and elimination of implicit GIL-dependent mutable patterns in dependency injection. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unsynchronized Module-Level State and Shared Mutable Dependencies Under Python 3.13 Free-Threading) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

FastAPI