Skip to main content

E-09 Context Manager

Brick E: Data, RAG, context, memory. French version: E-09-context-manager

1. Role and responsibilities​

Builds, compresses and budgets the context sent to the model, with provenance for each segment. Budget: B_context = W_model - B_output - B_reasoning - B_safety.

  • Compute the budget from the model window
  • Prioritize segments (system, skills, memory, RAG, history)
  • Compress (summary, truncation, drop) and record compression_actions
  • Compute classification_max

2. Position in the architecture and interactions​

  • Callers: Loop Controller (C-03), SDK.
  • Execution path: Synchronous.
  • Every interaction goes through the Control Plane API v1 (/v1) behind the API Gateway (D-14).

3. Open source component and developed part​

  • Open source: none (to develop)
  • Developed: Fully developed.
  • External integration through an adapter eai/integrations/<system>.py with an in-process fallback when the URL is not configured.

4. Data model​

Tables (schema eai, tenant_id RLS):

  • eai.run_steps (context_id)

Common columns: id uuid, tenant_id uuid NOT NULL, created_at, updated_at; RLS policy current_setting('app.tenant_id', true)::uuid.

5. Contract​

  • Applied contract: context-contract
  • JSON Schema: contracts/v1/context-contract.schema.json
  • Key fields: budget.window, budget.output_reserved, budget.reasoning_reserved, budget.safety_margin, budget.context_budget, budget.used, segments, compression_actions, classification_max
  • Validation: POST /v1/contracts/{name}/validate; manifest apply: POST /v1/manifests/apply.

6. API endpoints​

  • POST /v1/context/plan

Request POST /v1/context/plan:

{
"model_id": "gpt-4.1",
"window": 128000,
"output": 4000,
"reasoning": 8000,
"safety": 2000,
"segments": [
{
"kind": "history",
"tokens": 130000,
"priority": 2
}
]
}

Response:

{
"budget": {
"window": 128000,
"output_reserved": 4000,
"reasoning_reserved": 8000,
"safety_margin": 2000,
"context_budget": 114000,
"used": 113500
},
"compression_actions": [
{
"segment": 0,
"action": "summarize"
}
]
}

7. SDK examples​

Python:

from enterprise_ai import EnterpriseAI

client = EnterpriseAI(base_url="<control-plane-url>", api_key="eai_xxx_secret")
plan = client.context.plan(model_id="gpt-4.1", window=128000, output=4000, reasoning=8000, safety=2000, segments=segs)

TypeScript:

import { EnterpriseAI } from "@enterprise-ai/sdk";

const client = new EnterpriseAI({ baseUrl: "<control-plane-url>", apiKey: process.env.EAI_API_KEY });
const plan = await client.context.plan({ modelId: "gpt-4.1", window: 128000, output: 4000, reasoning: 8000, safety: 2000, segments });

Java:

EnterpriseAIClient client = EnterpriseAIClient.builder()
.baseUrl("<control-plane-url>")
.apiKey(System.getenv("EAI_API_KEY"))
.build();
ContextPlan plan = client.context().plan(req);

Go:

c, _ := eai.NewClient(eai.WithBaseURL("<control-plane-url>"), eai.WithAPIKey(os.Getenv("EAI_API_KEY")))
plan, _ := c.Context.Plan(ctx, map[string]any{"window": 128000, "output": 4000})

8. Configuration​

  • Environment variables: EAI_DATABASE_URL
  • Helm values keys: controlPlane.config, controlPlane.secrets

9. Security controls​

  • classification_max compared with the model allowed classification
  • Provenance mandatory per segment
  • Every request carries the tenant resolved from the credential; PostgreSQL RLS
  • Every mutation and policy decision writes a hash-chained audit event

10. Observability​

  • Prometheus metrics: eai_context_tokens_used, eai_context_compressions_total{action}
  • Traces: OpenTelemetry spans with GenAI semantic conventions (gen_ai.operation.name, gen_ai.agent.id, gen_ai.tool.name), attributes eai.tenant_id, eai.run_id, traceparent propagation.
  • Audit actions: no dedicated action, the context plan is computed statelessly; the model call that follows produces model.invoke.

11. FinOps units​

  • Input tokens saved by compression.
  • Emitted as Usage Event (POST /v1/finops/usage) with tenant, project, agent and run.

12. Failure modes and resilience​

  • Negative budget -> 422 context_budget_negative
  • Normalized errors (error envelope, message_i18n en/fr)

13. Standalone adoption (client of a client)​

  • enterprise_ai.llm.context, no other dependency.
  • SDK: enterprise_ai.privacy / dlp / knowledge_bases / memory / retention / lineage
  • Minimal deployment: Helm chart with controlPlane.enabled=true and PostgreSQL; disabled external components use their in-process fallback.

14. Acceptance tests​

  1. context_budget = window - output - reasoning - safety
  2. used <= context_budget after compression
  3. Cross-tenant access refused
  4. Error returned in fr with Accept-Language: fr

Governed RAG (kit L4)​

Only authorized, active (not quarantined) chunks of a still active source reach the context; every answer carries citations and lineage_run_id.