Skip to content

Intelligence (LLM Runtime)

The intelligence layer is Athena’s LLM dispatch system. It owns provider routing, circuit breaking, fallback, the tool loop, and budget tracking. All AI node executions pass through it.


Provider Matrix

Athena supports 15 LLM / media providers:

ProviderModalities
OpenAIchat, vision, tools, streaming
Anthropicchat, vision, tools, streaming, prompt caching
Google (Gemini)chat, vision, tools, streaming
Groqchat, tools, streaming
Coherechat, tools, streaming
Perplexitychat, streaming
xAI (Grok)chat, tools, streaming
OpenRouterchat, tools, streaming (model-dependent)
DeepSeekchat, reasoning, tools, streaming
Mistralchat, tools, streaming
MiniMaxchat, tools, streaming
Moonshot (Kimi)chat, tools, streaming
ZAi (GLM)chat, tools, streaming
XiaomiMimochat, tools, streaming
Fal.aimedia generation only (image/video)

Provider API keys are stored as workspace secrets and resolved at execution time. Key lookup order: group config → workspace secret → 401 Unauthorized.


Circuit Breaker

Each provider has an independent circuit breaker:

ParameterValue
Failure threshold to open5 failures in 60 s
Open duration before probe30 s
Probe successes to close2

States: Closed (normal) → Open (failures exceeded) → HalfOpen (probe) → Closed (2 successes) or back to Open (probe fails).

When a circuit is open, callers receive an ExternalService error immediately (no LLM call made). This error is in the fallback-trigger set, so node-level fallback still fires if configured.


Fallback Chain

Each AI node can optionally specify a (fallback_provider, fallback_model). If the primary call fails with a retryable error, the engine retries automatically with the fallback.

Errors that trigger fallback:

Error typeFallback triggered?
RateLimit (429)Yes
ExternalService (500 / 502 / 503 / 504, timeout, connection error)Yes
Circuit breaker openYes
Validation (400, prompt too long)No
Unauthorized / Forbidden / NotFoundNo

If the fallback also fails, the primary error is returned (fallback errors do not mask the root cause).


Tool Loop Limits

LimitDefaultEnv override
Rounds (LLM call ↔ tool result cycles)8ATHENA_TOOL_MAX_ROUNDS
Tool calls per round8ATHENA_TOOL_MAX_CALLS_PER_ROUND
Total tool calls across all rounds20ATHENA_TOOL_MAX_TOTAL_CALLS

Exceeding any limit terminates the loop with the last assistant message as output. The execution is marked Completed, not Failed.


Budget Tracking

Every execution tracks token and cost accumulation in execution variables:

VariableTypeUpdated
__accumulated_tokensu64After every LLM round
__accumulated_cost_microdollarsu64After every LLM round

Cost is calculated in microdollars from the model_pricing table (admin-managed, 5-minute in-process cache).

Budget enforcement happens before every AI node:

ActionEffect
EndExecution fails with TokenLimitExceeded or CostLimitExceeded
WarnLogged once via a latch variable; execution continues
HandoffExecution fails with TerminationReason::Handoff

Pre-call token estimation uses a 1.2x safety buffer: estimated = (prompt_chars / 4 × 1.2) + 4096 output tokens.


Anthropic Prompt Caching

Athena uses a direct Anthropic HTTP client (bypassing the SDK) to inject cache_control breakpoints into prompts. This is enabled by default and can be disabled via ATHENA_ANTHROPIC_PROMPT_CACHING=off.

Cache breakpoints are placed at:

  1. System prompt — if 4096+ characters
  2. Last tool definition — if more than one tool is defined
  3. Last assistant message — if 4096+ characters

Maximum 4 breakpoints per request. Cache hit rates are tracked in cost accounting (cache_read_input_tokens vs cache_creation_input_tokens).


Streaming vs Sync

Both execution paths share the same intelligence layer. The streaming path emits token chunks via SSE as the LLM generates them. The sync path waits for the complete response.

SSE token stream event types:

EventMeaning
textLLM text chunk
reasoningThinking / reasoning chunk (DeepSeek, Claude extended thinking)
tool_callLLM invoked a server-side tool
tool_resultServer-side tool returned a result
llm_finalLLM call complete with token usage

  • Architecture — how the execution pipeline works
  • Workflows — BFS loop that calls the intelligence layer
  • Credits — cost accounting and credit pre-deduction
  • Error Codesexternal_service_error, rate_limit codes