Architecture

This document describes the internal architecture of the Modeling Agent. For the full reference including all schemas, see Schema Reference.

System Overview

The BESSER Modeling Agent is a WebSocket-based conversational AI system built on the BESSER Agentic Framework. It connects the BESSER Web Modeling Editor (a React/TypeScript SPA) with OpenAI models, routed per call site through the model tier table in src/llm/model_config.py. Code generation is powered by BESSER generators (Django, Python, Java, SQL, SQLAlchemy, and more).

        graph TD
    FE["BESSER Web Modeling Editor<br/>(React/TypeScript SPA)"]
    FE -->|"WebSocket (JSON v2 protocol)"| PA

    subgraph AGENT["MODELING AGENT"]
        PA["Protocol Adapters"] --> UC["Unified Classifier<br/>(1 LLM call / message)"]
        UC --> SM["State Machine<br/>(10 states)"]
        SM --> EE["Execution Engine<br/>(plan + dispatch)"]
        EE --> ORCH["Orchestrator<br/>(planner + type resolver)"]
        ORCH --> DH["Diagram Handlers"]
        DH --> LE["Layout Engine"]
        EE --> FC["File Conversion"]
        EE --> GH["Generation Handler"]
    end

    DH --> LLM["OpenAI models<br/>(per-tier: classifier / generation / vision)"]
    SM --> RAG["ChromaDB<br/>(RAG store)"]
    RAG --> UML["UML Specs<br/>(PDF source)"]
    

Technology Stack

Component

Technology

Notes

Agent Framework

besser-agentic-framework[tensorflow] == 4.5.3

State machine, WebSocket platform, local intent classification. One file is vendored over the pip install — see patches/websocket_platform.py.

Routing LLM

MODEL_CLASSIFIER (default gpt-6-luna)

One structured-output call per message (the unified classifier)

Generation LLMs

MODEL_GENERATION_LARGE / _GUI / _SMALL, MODEL_REASONING

Structured diagram output; gpt-5 / o-series use reasoning_effort instead of temperature

Vision LLM

MODEL_VISION (default gpt-4o)

Image / PDF → diagram conversion

Local intent classifier

SimpleIntentClassifier (TensorFlow)

Free, trained at startup on each intent’s training sentences; exception-path and voice/text-event fallback only

RAG

LangChain + ChromaDB

Vector store over UML 2.5.1 specification

Speech-to-text

OpenAI whisper-1

Voice messages; language auto-detected unless pinned

Transport

WebSocket

Port 8765 (configurable)

Runtime

Python 3.11

python:3.11-slim base image

Architectural Layers

The system is organized into these layers, processed in order for each request:

  1. Protocol Layer (src/protocol/): Parses raw WebSocket messages into canonical AssistantRequest objects.

  2. Classification Layer (src/routing/unified_classifier.py): One structured-output LLM call per message, cached, returning the state-level intent plus every sub-routing field downstream code needs. See Intent Recognition.

  3. State Machine (modeling_agent.py + src/app/state_bodies.py): Routes requests to the appropriate handler based on that classification.

  4. Orchestration Layer (src/orchestrator/): Plans multi-step operations and resolves target diagram types.

  5. Execution Engine (src/execution/): Dispatches operations to diagram handlers and manages confirmation flows.

  6. Diagram Handler System (src/diagram_handlers/): Eight specialized handlers that generate diagram JSON via LLM calls. See Diagram Handlers.

  7. Utility Layer (src/utilities/): Model resolution, context building, metadata extraction, and layout helpers.

  8. LLM Abstraction (src/llm/): llm.provider.LLMProvider is the only code that calls the model SDKs: free text and JSON (complete()), structured outputs (parse()), streaming (stream()) and the file-conversion vision call (vision()), each with BYOK routing, retry, token tracking and the per-model sampling rules. Handlers and helpers get the shared LLMOpenAI instance injected and call it through the provider.

  9. BYOK Routing (src/llm/byok.py): Per-request routing of generation, structured-output, classification and conversational calls through a user-supplied API key, driven by a context var set at the WebSocket request boundary.

  10. Conversation Memory (src/memory/): Per-session conversation history with a rolling LLM summary of everything older than the verbatim window.

  11. Schemas (src/schemas/): Pydantic models for each diagram type, used by the structured-output pass of diagram handlers.

  12. Token Tracking (src/llm/tracking/): Process-wide token usage and estimated cost, logged periodically at INFO. Per-session buckets exist but no call site passes a session id, so only the global totals are populated — see Cost and Model Routing.

  13. Suggestion Engine (src/conversation/suggestions.py): Contextual next-step action suggestions returned to the frontend after each operation.

Every module lives in a top-level package under src/, and each package is one import layer: app → ingestion → execution → codegen / orchestrator → conversation → routing → memory / diagram_handlers → prompts → llm → utilities → protocol → common / schemas. A package imports only the ones below it (packages on one level do not import each other); lint-imports enforces this from the contracts in pyproject.toml. The one exception is a known cycle: a confirmed generation in codegen.generation_handler re-runs the planned operations in execution.

Prompts

The text sent to the model lives in src/prompts/, one module per diagram type or state: classifier (the routing rulebook), class_diagram, object_diagram, state_machine, agent_diagram, bpmn, gui, quantum_circuit, user_profile, component_diagram, deployment_diagram, fragments (rules several handlers share), planner, modeling_help, describe_model, fallback, uml_rag, file_conversion (file-upload schemas and conversion prompts), reply_language, spec_driven, model_operations and conversation_memory. A prompt that never changes is a constant (bpmn.MODIFY_SYSTEM_PROMPT_BPMN); one built from the request is a function of exactly those values (bpmn.reasoning_prompt(user_request)). Call sites import the module and look the prompt up by name (from prompts import bpmn as bpmn_prompts).

To change wording, edit the prompt module, never a call site. System prompts are composed once at import time and must stay byte-identical across calls (see Static prefix first); tests/test_prompt_snapshots.py pins a hash of every prompt, so update the hash in the same change. The package sits above schemas because some prompts embed schema vocabulary (component stereotypes, agent reply types).

Entry Point

modeling_agent.py performs the following startup sequence:

  1. Creates the BESSER Agent object and the WebSocket platform (use_ui=False)

  2. Calls the five init_* functions from agent_setup — init_llm, init_stt, init_rag, init_diagram_factory, init_intent_classifier_config

  3. Populates agent_context module-level globals

  4. Defines all 10 states and 10 intents (each intent with training sentences, required by the local Simple classifier)

  5. Calls state_bodies.register_all() to wire state bodies and transitions

  6. Starts the session reaper thread

  7. Calls agent.run()

It imports the agent’s packages by top-level name (from conversation import agent_context, from app.state_bodies import register_all). They resolve because the agent is installed with pip install -e .: pyproject.toml makes src/ the import root, locally, in CI and in the Docker image.

Shared Runtime Context

src/conversation/agent_context.py stores module-level globals populated at startup:

Variable

Type

Description

agent

Agent

BESSER Agent instance

gpt

LLMOpenAI

Structured / JSON call path. Defaults to the MODEL_CLASSIFIER tier, temp=0.2; generation-quality call sites override the model per call via the model= plumbing.

gpt_text

LLMOpenAI

Free-text mode, MODEL_CLASSIFIER tier, temp=0.4. Registered under the key <model>-text because BAF keys LLMs by name.

gpt_predict_json

Callable

LLMProvider(gpt).complete(..., json_mode=True), with an optional per-call model override

uml_rag

RAG | None

ChromaDB-backed RAG, None if unavailable

diagram_factory

DiagramHandlerFactory

Factory for all 8 diagram handlers

openai_api_key

str

Server API key from config.yaml

stt

OpenAISpeech2Text

Whisper speech-to-text for voice messages

All modules import these at call-time (not import-time) to ensure they are populated when user messages arrive.

State Machine and Intent Classification

The agent uses 10 states with corresponding intents. The authoritative classification comes from src/routing/unified_classifier.py — one structured-output LLM call per message, cached, read by the json_intent_matches transition condition. BAF’s own local SimpleIntentClassifier is only an exception-path and voice/text-event fallback. See Intent Recognition.

Intent

Target State

Behavior

hello_intent

greetings_state

Welcome message, quick patterns

create_complete_system_intent

create_complete_system_state

Multi-element system design

modify_model_intent

modify_model_state

Single element creation and editing existing elements

modeling_help_intent

modeling_help_state

Conceptual Q&A with LLM

describe_model_intent

describe_model_state

Analyze current project

uml_spec_intent

uml_rag_state

UML spec lookups via RAG

generation_intent

generation_state

Code generation routing (deterministic, smart, export, deploy, GitHub import)

decline_intent

decline_state

Acknowledge an opt-out without building anything

out_of_scope_intent

out_of_scope_state

Redirect a request for a non-software artifact

meta_question_intent

meta_question_state

Answer “what can you do / why use you” questions

Both modeling states (create_complete_system, modify_model) share the same body function _modeling_state_body() with different default_mode parameters.

Execution Engine

src/execution/ is the core dispatch layer (package with planning.py and model_operations.py; file attachments go through src/ingestion/file_handling.py first).

execute_planned_operations():

  1. Calls plan_assistant_operations() to get operation list

  2. Loops over operations:

    • type == "model" → execute_model_operation()

    • type == "generation" → handle_generation_request()

  3. If an operation returns None (pending confirmation), saves remaining ops

execute_model_operation() — the most complex function:

  1. Resolve diagram type (from operation or heuristic)

  2. Resolve operation mode (complete_system / modify_model — a modify_model op on a flow-style diagram that does not exist yet is promoted to complete_system)

  3. Existing-model guard (complete_system only)

  4. GUI generation-mode choice (GUINoCodeDiagram only)

  5. Handler lookup via diagram_factory

  6. Build modeling prompt (request + workspace context)

  7. Dispatch to handler method

  8. Inject metadata (diagramType, diagramId, replaceExisting)

  9. Send reply payload to frontend

  10. Record action in session history

  11. Run quality review

Confirmation Flows

Several confirmation flows pause execution and resume on the next user message. Their state lives in one session variable (common.pending_dialogs); resume_pending() in _common_preamble() hands the next message to the open dialogs in a fixed precedence (GUI choice, replace/keep or destructive confirmation, Spec-Driven confirmation, generator), and a generator menu or settings answer reaches the generation state body through routing.

Flow

Question asked

Complete-system confirmation (src/app/confirmation.py)

A model already exists — replace it, keep it, or use a new tab?

GUI generation-mode choice (src/app/confirmation.py)

“Basic CRUD pages” (one page per class, no LLM) or “Experimental AI design”?

Spec-Driven Agent confirmation (src/codegen/generation_handler.py)

Confirm before handing off to the LLM-authored Spec-Driven Agent

Plan-generation pause (src/codegen/generation_handler.py)

A mixed modeling + generation plan pauses after the modeling step and waits for an explicit “generate” before running the generator

Destructive-modification confirmation (src/execution/model_operations.py)

A modify_model plan that would delete most or all of the existing diagram asks before applying

Domain-mismatch confirmation

The smart request describes a different domain than the existing class diagram — confirm before rewriting

Whether the classifier treats the next message as an answer to a pending question or as an unrelated new request is itself part of the classification (pending_flow_action), so an off-topic instruction can never be swallowed by a half-finished flow.

Progress Events

For multi-step plans (2+ operations), progress events are sent to the frontend via reply_progress() so that the user sees real-time step indicators (e.g., “Step 1 of 3: Creating class diagram…”).

Request Planner

src/orchestrator/request_planner.py decomposes a user message into an ordered list of operations using a 3-tier approach:

  • Tier 0 – Fast heuristic regex patterns: A bank of compiled regular expressions matches common request shapes (e.g., “create a web app”, “generate Python code”, “design a state machine”). This tier handles ~90% of simple requests with zero LLM calls.

  • Tier 1 – Keyword-based fallback with intent-aware fast path: When no regex matches, a keyword-based classifier determines the operation type and target diagram without calling the LLM.

  • Tier 2 – LLM planner: Only genuinely complex multi-step requests that escape both fast paths are sent to the LLM for decomposition.

Two-Pass Generation

Diagram handlers use a two-pass strategy for complex requests (predict_two_pass_structured): a reasoning pass (free-text LLM call on the MODEL_REASONING tier to plan the design) followed by a structured pass (OpenAI Structured Outputs with Pydantic schema validation on a generation tier).

For simple requests the reasoning pass is skipped entirely, saving one full LLM round-trip. “Simple” is judged by the length of the raw user message (_TWO_PASS_MIN_LENGTH = 250 characters) rather than the enriched prompt — otherwise conversation history and the workspace-context block would push a trivial request onto the expensive path.

Session Identity

Conversation memory is keyed on memory_session_key() (src/memory/__init__.py), which prefers the v2 payload’s sessionId (AssistantRequest.session_id) because it survives WebSocket reconnects. It falls back to the BAF session id only when no parsed request is available — the BAF id changes on every reconnect without a stable user query param, which would silently drop all conversation context.

A memory lives as long as its conversation is in use: the reaper drops it after an hour with no new message and no keep-alive heartbeat from the tab that holds it (touch_memory; the heartbeat’s user_id is the tab’s sessionId). If the server lost it anyway (a restart, a tab left closed for over an hour), the client’s recentHistory on its first message after a reconnect rebuilds it (WebSocket Protocol Reference).

The frontend keeps that continuity by appending a persisted ?user_id= query parameter to the WebSocket URL. Because the two sockets a browser tab opens (the assistant widget and the workspace drawer) then share one session key, stock BAF (still in 4.5.3) would evict the live connection when either closed; patches/websocket_platform.py vendors an ownership-guarded delete plus reply-route-to-sender to fix this.

Rate Limiting, Retries and Caching

Rate Limiting: Handled by the provider API directly. HTTP 429 and 5xx responses are treated as transient by the retry layer; other 4xx are permanent and fail fast.

Shared-client retry (src/llm/retry.py): the shared server LLM’s raw SDK call is patched once, at the client’s network-call layer, so every path through it — BAF predict/chat and every LLMProvider call on the server key, vision included — inherits the same bounded backoff. MAX_ATTEMPTS = 4 (1 try + 3 retries), base delay 0.6 s, per-attempt cap 6 s, ±0.3 s jitter — roughly 5 s of backoff sleep per call, on top of the failed attempts themselves. The SDK’s own retries are disabled on these clients (max_retries=0) so the two do not multiply. BYOK’s per-request client is a separate object this patch never touches; it keeps the SDK default of 2 retries.

Handler-level retry (base_handler.predict_with_retry): jittered exponential backoff over 1 + max_retries attempts (default 1 retry), then the graceful-degradation chain below. Rate-limit, authentication and bad-request errors are not retried here and skip the degradation chain, so the user sees them. Worst cases are in Cost and Model Routing.

Parsed-Request Cache: parse_assistant_request() caches its result per-event using id(session.event) as the key, avoiding redundant JSON parsing within a single message cycle (it is called 3–5 times per message).

Classification Cache: get_or_classify() caches the unified classification on the same event-id key, so one incoming message costs exactly one classification call regardless of how many transition conditions ask.

Graceful Degradation

Every modeling operation follows a 4-level degradation chain:

  1. Primary LLM call (with retry)

  2. JSON repair via LLM

  3. Type-specific fallback generator

  4. Error response with retryable: true

No exceptions propagate to the caller, except provider rate-limit, authentication, bad-request and repeated-truncation errors: those reach execution/model_operations.py, which replies with an agent_error (rate_limit on the shared key offers adding an own API key).

Design Patterns

Module-Level Globals: All LLM handles and the diagram factory are stored in agent_context.py to avoid circular imports.

Protocol Decoupling: AssistantRequest separates protocol parsing from execution. Downstream code works only with typed Python objects.

Handler Extensibility: Adding a new diagram type requires implementing the 6 abstract methods of BaseDiagramHandler and registering the class in HANDLER_CLASSES. Layout, retry, structured output, and two-pass generation are inherited. See Diagram Handlers for the full checklist.

Per-Request BYOK Isolation: A user’s own API key never touches the shared LLM objects. It lives in a contextvars.ContextVar set and reset at the WebSocket request boundary, so concurrent sessions can never cross keys.

Deterministic Layout: LLMs never emit positions. The layout engine runs after every generation, ensuring collision-free visual presentation.