MATIH Platform is in active MVP development. Documentation reflects current implementation status.
2. Architecture
Agent Flow

Agent Flow

The agent flow is the core "Intent to Insights" workflow of the MATIH Platform. A user asks a natural language question, and the AI Service orchestrates a pipeline of specialized agents that classify intent, generate SQL, execute queries, analyze results, and recommend visualizations.


End-to-End Flow

Browser               AI Service           Query Engine    Trino
  |                      |                      |            |
  | POST /api/v1/ai/chat |                      |            |
  | "What was revenue    |                      |            |
  |  last quarter?"      |                      |            |
  |--------------------->|                      |            |
  |                      |                      |            |
  |           1. Establish tenant context        |            |
  |           2. Load/create session             |            |
  |           3. RouterAgent: classify intent    |            |
  |              --> Intent: SQL_QUERY           |            |
  |           4. SQLAgent: generate SQL          |            |
  |              Context: schema from catalog    |            |
  |              Context: MDL from semantic-layer|            |
  |              Context: similar queries (RAG)  |            |
  |                      |                      |            |
  |           5. Execute generated SQL           |            |
  |                      |--------------------->|            |
  |                      |                      |----------->|
  |                      |                      |  Trino     |
  |                      |                      |  executes  |
  |                      |                      |<-----------|
  |                      |  Result set          |            |
  |                      |<---------------------|            |
  |                      |                      |            |
  |           6. AnalysisAgent: interpret results|            |
  |           7. VizAgent: recommend chart       |            |
  |           8. Update session state            |            |
  |           9. Publish audit event (Kafka)     |            |
  |                      |                      |            |
  | Response             |                      |            |
  | {answer, sql,        |                      |            |
  |  data, chart_config} |                      |            |
  |<---------------------|                      |            |

Agent State Machine

The AI Service orchestrator manages the flow as a LangGraph state machine:

START
  |
  v
ROUTE (RouterAgent classifies intent)
  |
  +----> SQL_QUERY ------> GENERATE_SQL (SQLAgent)
  |                            |
  |                            v
  |                         EXECUTE_QUERY (via query-engine)
  |                            |
  |                            v
  |                         ANALYZE (AnalysisAgent)
  |                            |
  |                            v
  |                         VISUALIZE (VizAgent)
  |                            |
  |                            v
  |                         RESPOND
  |
  +----> ANALYSIS -------> ANALYZE (AnalysisAgent)
  |                            |
  |                            v
  |                         RESPOND
  |
  +----> DOCUMENTATION --> DOCS (DocumentationAgent)
  |                            |
  |                            v
  |                         RESPOND
  |
  v
END (session updated)

Agent Responsibilities

AgentInputOutputLLM Call
RouterAgentUser message + session contextIntent classificationYes
SQLAgentIntent + schema metadata + RAG examplesSQL queryYes
AnalysisAgentQuery results + original questionNatural language insightYes
VizAgentResults + insightChart type and configurationRule-based + LLM
DocumentationAgentQuestion about data termsDefinition and contextYes (RAG)

RAG Context Sources

The SQLAgent retrieves context from multiple sources to improve SQL generation accuracy:

SourceServiceContent
Schema metadataCatalog ServiceTable names, column names, data types
Business metricsSemantic LayerMetric definitions, dimension hierarchies
Similar queriesVector Store (Qdrant)Previously successful SQL for similar questions
Business termsOntology ServiceBusiness term definitions and relationships

Timing Breakdown

StepTypical Duration
Gateway processing2-5ms
Intent classification (RouterAgent)200-500ms
SQL generation (SQLAgent)500-2000ms
Query execution (Trino)50-5000ms
Result analysis (AnalysisAgent)300-1000ms
Visualization recommendation (VizAgent)100-300ms
Total (typical)1-8 seconds

Streaming Response

For long-running queries, the AI Service streams tokens via Server-Sent Events:

AI Service --> Redis Pub/Sub --> WebSocket Bridge --> Browser

SSE: data: {"token": "Revenue", "index": 0}
SSE: data: {"token": " last", "index": 1}
SSE: data: {"token": " quarter", "index": 2}
SSE: data: {"token": " was", "index": 3}
...
SSE: data: {"done": true, "sql": "SELECT ...", "data": [...]}

Session Management

Each conversation is tracked in a session:

Session FieldDescription
session_idUnique session identifier
tenant_idTenant scope
user_idAuthenticated user
messagesConversation history
contextAccumulated context (schemas, queries)
stateCurrent state machine state

Follow-up questions use session context to modify queries without re-specifying the full context.


Related Pages