> ## Documentation Index
> Fetch the complete documentation index at: https://splinter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# The Flow

> How every LLM call flows through Splinter

## The Core Flow

Every LLM call in Splinter follows this exact path:

```mermaid theme={null}
flowchart TB
    Agent[Agent calls gateway.call]
    
    Gateway[<b>GATEWAY</b>]
    
    Control[<b>1. CONTROL CHECKS</b><br/>✓ Budget OK?<br/>✓ Steps OK?<br/>✓ Time OK?<br/>✓ Tool access OK?<br/>✓ Loop detected?]
    
    Route[<b>2. ROUTE TO LLM</b><br/>• OpenAI / Anthropic / Gemini<br/>• Get response<br/>• Track cost]
    
    Coord[<b>3. COORDINATION</b><br/>• Update shared state<br/>• Checkpoint for resume]
    
    Response[Return response]
    NextAgent[Next agent]
    
    Agent --> Gateway
    Gateway --> Control
    Control --> Route
    Route --> Coord
    Coord --> Response
    Response --> NextAgent
    
    style Gateway fill:#8B0000,stroke:#6c0404,stroke-width:3px,color:#fff
    style Control fill:#FEE2E2,stroke:#6c0404,stroke-width:2px,color:#000
    style Route fill:#FEE2E2,stroke:#6c0404,stroke-width:2px,color:#000
    style Coord fill:#FEE2E2,stroke:#6c0404,stroke-width:2px,color:#000
    style Agent fill:#DBEAFE,stroke:#3B82F6,stroke-width:2px,color:#000
    style Response fill:#DBEAFE,stroke:#3B82F6,stroke-width:2px,color:#000
    style NextAgent fill:#DBEAFE,stroke:#3B82F6,stroke-width:2px,color:#000
```

## Step by Step

### 1. Agent Calls Gateway

```python theme={null}
# Agent.run() internally calls:
response = await gateway.call(
    agent_id="researcher",
    provider=LLMProvider.OPENAI,
    model="gpt-4o-mini",
    messages=[...],
    state=shared_state,
)
```

### 2. Control Checks

Gateway asks the Control Layer:

```python theme={null}
# Internal to Gateway
self._control.check_before_call(agent_id, tools)
```

This checks:

* **Budget**: Have we exceeded `max_budget`?
* **Steps**: Have we exceeded `max_steps`?
* **Time**: Have we exceeded `max_time_seconds`?
* **Tools**: Is this agent allowed to use these tools?
* **Loops**: Is the agent stuck in a loop?

<Warning>
  If any check fails, an exception is raised and the call is blocked.
</Warning>

### 3. Route to LLM

If Control says OK, Gateway routes to the provider:

```python theme={null}
# Internal to Gateway
provider = self.get_provider("openai")
response = await provider.complete(request)
```

### 4. Record Metrics

After the call, Gateway records:

```python theme={null}
# Internal to Gateway
self._control.record_call(response, agent_id, state)
```

This tracks:

* Cost (tokens × price)
* Token usage
* Latency
* Call history

### 5. Coordination

Gateway updates coordination:

```python theme={null}
# Internal to Gateway
self._coordination.checkpoint(
    workflow_id=workflow_id,
    step=step,
    agent_id=agent_id,
    state=state,
)
```

### 6. Return Response

The response flows back to the agent, which parses it and continues.

## Why This Matters

<CardGroup cols={2}>
  <Card title="Safety">
    Every call is checked. No agent can exceed limits.
  </Card>

  <Card title="Observability">
    Every call is tracked. You know exactly what happened.
  </Card>

  <Card title="Resumability">
    Every step is checkpointed. Failed runs can resume.
  </Card>

  <Card title="Coordination">
    State is shared. Agents work together, not against each other.
  </Card>
</CardGroup>
