---
title: "ContextBuilderRegistry"
manual: "AI Assistant"
version: "main"
source: "Developers/Registries/ContextBuilderRegistry.rst"
rendered: "2026-10-03T14:54:45+00:00"
---

# ContextBuilderRegistry {#contextbuilderregistry}

## Purpose {#purpose}

Collects structured prompt context sections from all registered context
builders that support the current pipeline processor type.

The registry is used by prompt building to keep prompt sections modular. Each
builder can contribute one or more `PromptSection` instances for a specific
processor type, for example query optimization, retrieval context optimization
or answer generation.

## Required interface {#required-interface}

`Madj2k\AiCore\Assistant\Prompt\Context\Builder\ContextBuilderInterface`

## Service registration {#service-registration}

Register implementations through the context builder service tag:

```yaml
services:
  Vendor\Extension\Assistant\Prompt\Context\Builder\MyContextBuilder:
    tags:
      - name: 'aiassistant.assistant.prompt.context_builder'
```

## Selection {#selection}

The registry calls `supports()` with the current
`AssistantPipelineProcessorType`. Builders that return `true` can add
sections through `build()`.

```php
use Madj2k\AiCore\Assistant\Enum\AssistantPipelineProcessorType;

public function supports(AssistantPipelineProcessorType $type): bool
{
    return $type === AssistantPipelineProcessorType::AnswerGenerator;
}
```

## Output {#output}

`build()` must return an array of `PromptSection` objects. Empty sections are
ignored by the registry. Non-empty sections are sorted by their priority before
they are used for the final prompt context.

## Implementation rules {#implementation-rules}

-   Keep builders focused on one context concern.
-   Read from the shared assistant `Context` instead of loading unrelated state.
-   Return structured `PromptSection` objects instead of formatted prompt text
    where possible.
-   Use priorities deliberately so project-specific sections can be placed before
    or after the built-in context sections.
