---
title: "Universal Extensibility: The DI System | CodeWeaver Docs"
description: CodeWeaver is a code search engine for AI agents, designed to provide precise results that give agents exactly what they need for their tasks -- no more, no less.
url: "https://docs.knitli.com/concepts/di-system"
type: static
generatedAt: "2026-04-17T17:21:09.917Z"
---

# Universal Extensibility: The DI System
       [Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[View in Markdown](/codeweaver/concepts/di-system.md)       [Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F)[Share on X](https://x.com/intent/tweet?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F&text=Universal%20Extensibility%3A%20The%20DI%20System)[Share on Threads](https://threads.net/intent/post?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F&text=Universal%20Extensibility%3A%20The%20DI%20System)[Share on Bluesky](https://bsky.app/intent/compose?text=Universal%20Extensibility%3A%20The%20DI%20System%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F)[Share on Reddit](https://reddit.com/submit?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F&title=Universal%20Extensibility%3A%20The%20DI%20System)[Share on Hacker News](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F&t=Universal%20Extensibility%3A%20The%20DI%20System)[Share on Email](mailto:?subject=Universal%20Extensibility%3A%20The%20DI%20System&body=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F)[Share on WhatsApp](https://wa.me/?text=Universal%20Extensibility%3A%20The%20DI%20System%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F)[Share on Telegram](https://t.me/share/url?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fconcepts%2Fdi-system%2F&text=Universal%20Extensibility%3A%20The%20DI%20System)
# Universal Extensibility: The DI System
[Section titled “Universal Extensibility: The DI System”](#universal-extensibility-the-di-system)
> **TL;DR:** This module handles Universal Extensibility via Dependency Injection (DI). Use it when you need to register services or inject them into functions. It decouples implementation from usage, allowing you to swap providers with zero code changes.

CodeWeaver uses a lightweight, FastAPI-inspired Dependency Injection (DI) system. This architecture makes the platform extremely extensible—you can swap an embedding provider, a vector store, or even core services by simply updating your configuration.

---

## Core Components
[Section titled “Core Components”](#core-components)
The DI system relies on three primary markers to manage services:

### 1. `@dependency_provider` (The Registration Decorator)
[Section titled “1. @dependency_provider (The Registration Decorator)”](#1-dependency_provider-the-registration-decorator)
Use this decorator to register a factory function or a class as a provider for a specific type. By default, providers are **singletons** (one instance for the entire application lifetime).


**

```
from codeweaver.core.di import dependency_provider
@dependency_provider(MyService, scope="singleton")async def create_my_service() -> MyService:    return MyService(api_key="secret")
```

### 2. `INJECTED` (The Type Sentinel)
[Section titled “2. INJECTED (The Type Sentinel)”](#2-injected-the-type-sentinel)
The `INJECTED` sentinel serves as a default value for function parameters. It tells the DI container: “Don’t use a literal value here; look up the correct provider based on the type hint.”


**

```
async def search_code(service: MyService = INJECTED) -> None:    # 'service' is automatically populated by the DI container    result = await service.search("query")
```

### 3. `Depends` (The Injection Marker)
[Section titled “3. Depends (The Injection Marker)”](#3-depends-the-injection-marker)
For more complex scenarios, use the `Depends` marker within an `Annotated` type hint. This allows you to specify a specific factory or change the injection scope.


**

```
from typing import Annotatedfrom codeweaver.core.di import Depends, INJECTED
async def advanced_search(    service: Annotated[MyService, Depends(custom_factory)] = INJECTED) -> None:    ...
```

---

## Lifecycle Scopes
[Section titled “Lifecycle Scopes”](#lifecycle-scopes)
CodeWeaver manages objects based on their required lifetime:

| Scope | Description | Use Case |
| --- | --- | --- |
| **`singleton`** | One instance per application lifetime. | Shared caches, database clients, settings. |
| **`request`** | One instance per search request or operation. | Telemetry trackers, request-specific state. |
| **`function`** | A new instance created for every call. | Temporary files, ephemeral workers. |

---

## Advanced Features
[Section titled “Advanced Features”](#advanced-features)
### Collection Registration
[Section titled “Collection Registration”](#collection-registration)
Sometimes a type needs multiple providers (for example, many different embedding models). Use `collection=True` to register a factory that returns a list of instances.


**

```
@dependency_provider(Capability, scope="singleton", collection=True)def get_all_capabilities() -> Sequence[Capability]:    return [Capability("Search"), Capability("Rerank")]
```

### Automatic Local Fallback
[Section titled “Automatic Local Fallback”](#automatic-local-fallback)
The DI system handles resilience by allowing “backup” providers. If a primary cloud provider fails, the system automatically falls back to a locally registered provider (like FastEmbed or Sentence-Transformers) without interrupting the agent’s workflow.

---

## Why This Matters
[Section titled “Why This Matters”](#why-this-matters)
 1. **Zero-Code Provider Swapping:** Switch from OpenAI to Anthropic or Qdrant to Memory by changing a single line in `codeweaver.toml`.
 1. **Testability:** Inject mock services during testing without monkey-patching your codebase.
 1. **Predictability:** The DI system validates dependencies at startup, catching “Missing Provider” errors before they reach production.