Webhook icon
Schedule icon
ChatCompletion icon
Ollama icon
If icon
GoogleGemini icon
SlackIncomingWebhook icon
Log icon
Script icon
OutputValues icon

Build an LLM Gateway with Local vLLM, Gemini Cloud Fallback, and Langfuse Tracing

Build an LLM gateway in Kestra. Query local vLLM/Ollama, fail over to Google Gemini on errors, and trace latency, cost, and routing in Langfuse.

Categories
AI

Running large language models locally using engines like vLLM or Ollama eliminates per-token API charges and keeps proprietary prompts on-premises. However, local GPU nodes are susceptible to CUDA out-of-memory errors, model reloading delays, and unexpected daemon downtime. This blueprint orchestrates an intelligent LLM gateway that routes requests to a local instance first, gracefully fails over to Google Gemini when local capacity degrades, and records unified telemetry in Langfuse for centralized observability.

How it works

  1. An authenticated Webhook trigger (on_request) serves as an HTTP entrypoint for applications, while a disabled-by-default Schedule trigger (nightly_health_check) enables periodic synthetic gateway testing.
  2. call_local_vllm (io.kestra.plugin.ai.completion.ChatCompletion) queries the local inference server using the io.kestra.plugin.ai.provider.Ollama provider. It is configured with allowFailure: true and a strict 15-second execution timeout, ensuring that hung local daemons do not block the pipeline.
  3. check_fallback (io.kestra.plugin.core.flow.If) evaluates whether local inference returned output:
    • If local inference failed (outputs.call_local_vllm.textOutput is not defined), call_gemini_fallback (io.kestra.plugin.ai.provider.GoogleGemini) routes the prompt to Google Gemini (gemini-3.1-flash-lite), while notify_slack_fallback (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) informs engineers that failover was engaged.
    • If local inference succeeded, log_local_success records a clean confirmation log.
  4. record_langfuse_trace (io.kestra.plugin.scripts.python.Script) runs an isolated Python container that connects to Langfuse, logs prompt and completion tokens, flags whether fallback was triggered, and records the active model name and execution ID.
  5. gateway_output (io.kestra.plugin.core.output.OutputValues) exposes the final text, provider name, and Langfuse trace URL as structured execution variables.
  6. A root-level errors safeguard dispatches an urgent Slack alert if both local and cloud tiers fail simultaneously.

What you get

  • Zero-token cost baseline: Routes high-volume requests to local hardware whenever available.
  • Zero-downtime reliability: Automatic failover to Google Gemini handles local server crashes, reboots, or capacity bottlenecks.
  • Centralized LLMOps observability: Every prompt, response, latency metric, and routing decision is searchable in Langfuse.
  • Instant incident awareness: Slack notifications alert AI engineers whenever the gateway relies on fallback capacity.

Who it's for

  • AI Engineers deploying hybrid local/cloud generative AI applications.
  • Platform Engineers building internal developer LLM gateways and API proxies.
  • FinOps teams managing and minimizing commercial LLM API expenditure.

Why orchestrate this with Kestra

Building LLM routing logic directly into application code often creates tightly coupled dependencies, unhandled timeout bugs, and fragmented logs. Kestra decouples gateway routing into a declarative YAML workflow. You benefit from automatic retries, timeout enforcement, secret masking via {{ secret(...) }}, execution trace histories, and built-in event-driven webhooks without managing a dedicated reverse-proxy cluster.

Prerequisites

  • A local Ollama or vLLM server running on your network (accessible from the Kestra worker).
  • A Google AI Studio API key with access to Gemini 3.1 Flash-Lite.
  • A Langfuse account (Cloud or self-hosted) with project API keys.

Secrets

Configure the following secrets in your Kestra namespace:

  • GEMINI_API_KEY: API key for Google Gemini fallback completions.
  • LANGFUSE_PUBLIC_KEY: Langfuse project public API key.
  • LANGFUSE_SECRET_KEY: Langfuse project secret API key.
  • LANGFUSE_HOST: Langfuse host URL (e.g., https://cloud.langfuse.com or self-hosted instance).
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint for triage alerts.
  • WEBHOOK_KEY: Secret authentication key for the gateway webhook trigger.

Quick start

  1. Configure the required secrets in your Kestra namespace settings.
  2. Ensure your local Ollama or vLLM instance is running and has the model pulled (e.g., ollama run llama3.2:3b).
  3. Import this blueprint into your Kestra instance.
  4. Click Execute with the default inputs. If local Ollama is reachable, it generates a response locally; if unreachable, watch Kestra gracefully switch to Gemini and view the resulting trace link in the Outputs tab.
  5. Connect external applications by posting JSON payloads to the gateway webhook URL.

How to extend

  • Add token bucket rate limiting using Kestra's concurrency limits to prevent overwhelming the local GPU.
  • Swap Google Gemini for Anthropic Claude or OpenAI GPT-4o as the secondary or tertiary fallback tier.
  • Integrate Langfuse prompt management to fetch versioned prompt templates dynamically before inference.
  • Store generated responses and metadata in a vector database or PostgreSQL warehouse for downstream analytics.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.