Dify and LiteLLM proxy integration

Route every Dify model call through a self-hosted LiteLLM proxy using the official marketplace plugin, with virtual keys, model aliases, budgets and fallbacks planned before go-live.

On this page

Dify and LiteLLM proxy integration: routing all model traffic through one gateway

Most teams running several Dify apps against several model providers have no single place to see spend, cap usage or rotate keys, because each provider credential sits in Dify's model settings. The official LiteLLM plugin connects Dify to a LiteLLM Proxy as one OpenAI-compatible endpoint, so every app can call a single gateway that fans out to the providers behind it. The plugin is published by langgenius and verified by Dify on the Dify Marketplace. In this model the proxy, not Dify, holds the provider credentials, budgets and rate limits, and Dify only needs a key for the proxy.

The plugin supports LLMs for chat and completion, including streaming and tool calls, and text embedding models. You configure it with three inputs: the proxy base URL, a *virtual key* or master key, and the *model alias* defined in the proxy configuration. If the proxy runs without authentication, the key field stays empty, which is acceptable only on a private network segment. Confirm before go-live that every model type your apps need, especially embeddings used by knowledge bases and rerank steps, is exposed through the proxy's model list, because a missing alias fails at runtime rather than at setup.

Decide early which key scope each Dify app or workspace receives. Virtual keys can be limited to specific models and carry budgets and request limits, so a reporting app can be held to a cheaper alias than a production agent. Treat the master key as an administrative secret that stays out of Dify entirely, and plan how aliases change when you swap a provider, since app configurations reference the alias and not the underlying model. Ask your team to confirm how the proxy is backed by PostgreSQL and Redis before relying on virtual keys for spend control, because those features depend on that infrastructure.

What you need in place before connecting Dify to LiteLLM

The gateway sits between your Dify api and plugin daemon and every model provider, so its database, cache, network path and credentials need to exist before the Dify plugin is configured. Confirm each item below in your own environment, and confirm the Dify and LiteLLM versions against their release pages on the day you install.

A self-hosted Dify release

Dify runs in your infrastructure with the Docker Compose layout, which needs at least 2 CPU cores, 4 GB of RAM and Docker Compose 2.24.0 or later. Pick the Dify release on the GitHub releases page at install time, since minor versions arrive roughly monthly. Check the upgrade guide for that version and take a backup first, because the plugin daemon that carries the LiteLLM plugin is part of that stack.

LiteLLM proxy with PostgreSQL

Virtual keys, per-key budgets and spend tracking require a PostgreSQL database, so a proxy without one can only use a master key. Run the proxy on a version you can upgrade to, and confirm the release you pick on the LiteLLM releases page. Keep LITELLM_MASTER_KEY in a secret manager, since it must start with sk- and grants full administrative access.

Redis 7.0 or newer for multiple instances

A single proxy instance can run without Redis, but the moment you run more than one, rate limits, router state and cache must be shared. LiteLLM's production guide asks for Redis 7.0 or newer in that case. Size the proxy at roughly 1 vCPU and 4 GiB of memory per worker as a starting point, then measure it under the traffic your Dify apps generate.

Provider accounts behind the proxy

The gateway holds the upstream keys, so each provider account (OpenAI, Anthropic, Gemini, Bedrock, Azure and others you approve) must exist before you define model aliases. Decide which accounts the proxy owns, who can rotate them, and which spend limits they carry. Dify will only see the aliases, so a provider change should not require Dify edits.

Network path from Dify to the proxy

The Dify api and plugin daemon must reach the proxy base URL over the network, and the proxy must reach each provider endpoint. Dify's SSRF proxy blocks internal and private IP ranges by default, so a gateway on a private address needs a deliberate allowance rather than an open route. Test reachability from inside the Dify containers, not from your workstation.

TLS and secret handling

Virtual keys and the master key travel in request headers, so the Dify-to-proxy and proxy-to-provider paths should use TLS with certificates your team controls. Confirm the Dify SECRET_KEY is set before first use, because changing it later logs out all users. Store the plugin credential only in Dify's model provider settings, and keep the proxy's own configuration out of shared repositories.

How to configure the LiteLLM plugin in Dify, step by step

This sequence moves from proxy configuration to verified Dify model calls, so every model alias Dify uses is defined in the gateway first and every step closes with a check you can see.

  1. Define model aliases in the proxy configuration

    Before touching Dify, decide which models your teams may use and give each one a stable alias in the LiteLLM proxy configuration, mapping the alias to a provider model you have approved. Keep aliases stable and descriptive, because Dify stores the alias name, not the provider model, so renaming one later breaks every app that uses it. Your platform owner and the model owner should sign off on this list. You are done when the proxy starts cleanly and a direct request to the proxy using one alias returns a completion from the intended provider.

  2. Create a test virtual key scoped to the aliases

    Create a virtual key in the LiteLLM proxy that is restricted to the aliases from the previous step and nothing else. Virtual keys require a PostgreSQL database, so confirm the proxy is backed by one before you start. Set a conservative budget and rate limit for this test key, since it is only for validating the integration. Store the key in your secret manager rather than in a notes file. You are done when a request using this key succeeds for an approved alias and is rejected for an alias outside its list.

  3. Install the LiteLLM plugin from the Dify Marketplace

    In the Dify console, open the Marketplace and install the LiteLLM plugin published by langgenius, which Dify verifies. Dify runs plugins through its plugin daemon, so confirm your self-hosted instance can reach the marketplace or that you have an approved offline plugin process for your environment. Install it into the workspace that will use the models. You are done when the plugin appears in the workspace's installed plugins with the expected publisher.

  4. Enter the proxy base URL and key

    Open the LiteLLM plugin configuration and enter the base URL of your proxy, using an address the Dify api and worker containers can reach on your network. Enter the test virtual key in the key field. Leave the key field empty only if the proxy runs without authentication, which is rarely appropriate for a shared gateway. Save the configuration and confirm the plugin reports a successful connection. You are done when the save succeeds without a connection error and the proxy access log shows the connection attempt.

  5. Register each alias as a Dify model

    Add each approved alias as a model under the LiteLLM provider, selecting the model type that matches its role: chat or completion for language models, and text embedding for embedding models. Use the alias exactly as defined in the proxy configuration, since a mismatch produces failed calls that look like provider errors. Enter any context and output limits you have confirmed for that model. You are done when each alias appears in the model list with the correct type and a test prompt in the Dify model test panel returns output.

  6. Set workspace default models

    Choose default chat and embedding models for the workspace from the aliases you registered, so new apps and knowledge bases inherit gateway-routed models instead of a direct provider key. Review whether any existing app still points at a direct provider credential and reconfigure it to use an alias. Record which alias is the default and who approved it. You are done when a newly created test app uses a gateway alias by default and an existing app's model panel shows the alias rather than a direct provider.

  7. Confirm calls appear in proxy logs

    Run a test workflow or chat in Dify that calls each registered alias, including one streaming request and, if your use case needs them, one tool-calling request, since the plugin supports both. Then open the LiteLLM proxy logs or spend view and find each call under the test virtual key with the expected alias and model. Check that a call outside the key's allowed aliases is refused. You are done when every alias produces a visible proxy record and the scope restriction behaves as configured.

  8. Replace the test key with a planned key structure

    Once the integration is verified, replace the test key with keys issued per application or team, each with its own budget, rate limits and alias list, and store the master key in a secret manager. Remove the test key from the plugin configuration and revoke it at the proxy. Ask your LiteLLM admin about the budgets and limits you will need, and confirm which features of your chosen LiteLLM edition you rely on. You are done when Dify uses only planned keys, the test key returns an authorization failure, and the key inventory is recorded.

How the gateway changes spend tracking, budgets and failover

A LiteLLM proxy changes who decides how much a Dify app can spend and where its requests go. Dify only sees the model alias you expose through the LiteLLM plugin. Every budget, rate limit, model restriction and routing rule lives in the LiteLLM proxy, so your team has to configure each of them there. Nothing in the Dify console enforces them by default. A direct provider key gives you one shared bill and no per-application limits, while a virtual key issued by the proxy can be scoped to a single Dify workspace or application, with its own budget and model list. Virtual keys require a PostgreSQL database in the proxy, so plan that database before you issue keys.

The proxy controls several limits that Dify cannot set on its own. Per-key, per-user and per-team budgets cap spend, while TPM and RPM limits cap token and request throughput. Temporary budget increases let an owner raise a limit for a set period without reissuing the key, and restricting a key to specific models stops a Dify app from calling a model it was never approved to use. Confirm these settings in the LiteLLM virtual keys documentation for your proxy version, and test each one with a deliberately low limit before production. Decide who may approve budget increases and record that decision outside the proxy.

Failover is also a LiteLLM setting. The router can use simple-shuffle (the recommended strategy), usage-based, latency-based, least-busy or cost-based selection, and it can retry failed calls, cool down a deployment after repeated failures, order fallbacks by priority and keep sessions on the same deployment through session affinity. Pick the strategy with your cost and latency targets in mind, then rehearse a provider outage by disabling one deployment and checking that Dify answers still arrive. Dify does not know which provider served a request, so confirm that your logs and the LiteLLM routing documentation give you the evidence you need.

Single proxy or multi-instance: which layout fits your team

A single LiteLLM proxy instance is the simplest layout for a pilot, a proof of concept or one Dify environment serving a small group of builders. The proxy still needs a PostgreSQL database if you want virtual keys, since LiteLLM's virtual keys docs require it for per-key, per-user and per-team budgets and rate limits. A single instance keeps router state and cache in memory, so Redis is optional at this size. Use the pilot to confirm how your models, aliases and budgets behave before you add redundancy.

  • Database choice: Run PostgreSQL as the state store from the start. Do not plan around SQLite for anything that holds virtual keys or spend records.
  • Redis not yet required: With one proxy instance, shared rate limits and router state across replicas do not apply, so Redis can wait until a second instance exists.
  • Narrow key scope: Issue virtual keys restricted to the model aliases the pilot needs, so a test app cannot reach every provider.
  • Documented upgrade path: Record the configuration and database schema version so moving to several instances later is a planned change.

Before you rely on a pilot's numbers, confirm with your LiteLLM version which features you depend on, using the LiteLLM production best practices as the reference for the single-instance baseline.

Which Dify and LiteLLM settings map to each other

This table matches each field in the Dify LiteLLM plugin to its counterpart in the LiteLLM proxy, with the owner of each value and what changes when it moves. Read it before you create keys or rename aliases, because a change on the proxy side must be mirrored in Dify.

Dify plugin fieldLiteLLM counterpartWho owns the valueEffect of rotation or renaming
Base URLAddress of the LiteLLM proxy server, including its OpenAI-compatible pathPlatform team that runs the proxyChanges only when the proxy moves or gets a new hostname; update the plugin configuration in each workspace that uses it
Virtual or master keyVirtual key issued per app, team or user, or the master key set through LITELLM_MASTER_KEYPlatform team issues keys; the master key should stay in a secret managerRotating a key breaks every Dify model that uses it until the new value is entered; master keys should not be shared with app builders
Model aliasmodel_name entry in the proxy configuration that routes to one or more provider modelsPlatform team defines aliases; app builders select them in DifyRenaming an alias on the proxy makes the Dify model fail until the alias is reselected; keep old aliases during a cutover
Model type (LLM or text embedding)Chat or completion model entry versus embedding model entry in the proxy configurationDify builder selects, proxy team confirms the entry is an embedding modelEmbedding models change vector dimensions; switching an embedding alias to a different model usually means re-indexing knowledge bases
Authentication disabledProxy started without a master key or virtual key requirementPlatform team decides, usually only for isolated test setupsEnabling authentication later means every Dify model entry must receive a key, or calls will be rejected
Fallbacks and routingRouter settings such as priority ordering and retries in the proxyPlatform team configures; Dify sees only the aliasChanging fallback order changes which provider answers, so re-check output behavior and data handling for each provider

Securing the Dify to LiteLLM path

Every Dify model call that passes through LiteLLM carries credentials, prompts and spend data across two services. The cards below cover the decisions that keep that path scoped, encrypted and reachable only by the services that need it. Each card names a setting to review or a question to put to the vendor before go-live.

Scope keys per Dify use

Give Dify a LiteLLM virtual key rather than the master key. A virtual key can carry its own budget, request limits and a list of allowed models, so a misconfigured or compromised Dify workspace cannot reach every provider behind the proxy. Create one key per Dify environment or workspace where you need separate spend tracking. Confirm in your LiteLLM version which limits apply to embedding models, since the Dify plugin uses both LLM and embedding calls.

Protect the master key

LITELLM_MASTER_KEY is the administrative credential for the proxy and must start with sk-. Store it in a secret manager and inject it at runtime, not in the Dify .env file or in a Compose file under version control. Limit who can read it, because it can create virtual keys and change spend limits. Rotate it on a schedule you can document, and confirm how rotation affects existing virtual keys in your proxy version.

Limit outbound reach with SSRF_PROXY

Dify's SSRF_PROXY settings route outbound requests through a proxy that blocks internal and private IP ranges. A LiteLLM proxy on your internal network is a private address, so it will not be reachable through that filter by default. Add a narrow allowance for the proxy host and port only, and confirm the exact configuration against the Dify environment reference for your release. Keep every other internal host blocked so a workflow HTTP node cannot reach the same network.

Encrypt traffic between Dify and the proxy

Prompts, completions, tool arguments and virtual keys cross the link between Dify and LiteLLM in each request. Terminate TLS at the proxy or an internal load balancer and set the Dify base URL to the https endpoint. Use a certificate issued by an internal CA that the Dify containers trust, and test the trust chain from inside the api and worker containers, since a failure there shows up only as failed model calls.

Understand the SECRET_KEY dependency

Dify uses SECRET_KEY to encrypt OAuth credentials and to sign sessions. Provider API keys and virtual keys saved in Dify are stored encrypted, so the LiteLLM key is only as safe as the Dify secret that protects it. Changing SECRET_KEY after deployment logs out all users, and you should confirm with your Dify version whether stored model provider credentials can still be decrypted after a change. Back up the value with your database backup procedure.

Review LiteLLM Enterprise controls

The free admin UI SSO covers up to 5 users from v1.76.0, and audit logs, IP allowlisting, SCIM and secret manager integrations with AWS, Azure, Google, HashiCorp Vault and CyberArk sit in the Enterprise tier. If your security team needs named admin accountability, IP restriction on the proxy or managed secret retrieval, ask LiteLLM for a current quote and confirm which features your deployment model includes. Treat these as options to verify, not as settled controls.

How to test that every model call goes through the gateway

The test you care about is whether any Dify call can reach a model provider without passing through the LiteLLM proxy. Start by inventory: open each Dify app and workflow, and confirm that every LLM node, agent node and knowledge base embedding setting points to a model alias defined in the proxy configuration rather than a provider the workspace configured directly. Repeat the check for the workspace default models, because a default that still points at a direct provider will quietly bypass the gateway for new apps. Record the alias list as the expected state, then compare it with what the proxy's request logs show after a short test session.

Then run functional tests through the gateway. Send a streaming chat request and a request that triggers a tool call through an agent or a workflow with a tool node, and confirm both complete and appear in the proxy logs. Run an embedding test by indexing a small test document into a knowledge base that uses a proxy embedding alias, then run a retrieval query against it. Each test should produce a spend entry tied to the virtual key you assigned to that Dify workspace or app, so you can confirm attribution as well as delivery.

Next test the controls. Deliberately exceed a small test budget on a non-production virtual key, or restrict a key to a single model and request a different one, and confirm the proxy rejects the call with a clear error that Dify surfaces to the user. Simulate a provider outage by disabling or misconfiguring one provider behind a model group, then confirm the router falls back to the next deployment in the priority order you defined. Finally, remove or restrict the provider API keys that Dify or other systems could still use directly, and attempt a call with them to prove the bypass path is closed. Confirm spend totals per key and per team in the admin views before you sign off.

Common LiteLLM and Dify failures and their fixes

This table pairs the failures teams most often hit when Dify calls a LiteLLM proxy with the cause behind each one and the fix to try first. Read it by symptom: match the error you see in Dify or the proxy logs to a row, then check the cause before changing configuration. Most failures trace back to a mismatch between Dify's plugin settings and the proxy's own configuration.

SymptomLikely causeFix
Plugin connection refusedDify cannot reach the proxy base URL from its api or worker containers, often because the URL uses localhost or a wrong port, or TLS is misconfiguredUse an address the Dify containers can resolve and reach, such as the proxy service name on the shared Docker network or an internal DNS name. Confirm the scheme, port and certificate trust chain match what the proxy serves
Authentication error from the proxyThe virtual key in the plugin is wrong, expired, revoked or copied with extra whitespace, or a master key is expected but not enteredGenerate or confirm the key in the proxy, paste it into the plugin without spaces, and check that the key is allowed to use the model alias in use. Leave the key empty only if the proxy has no authentication enabled
Model not foundThe alias entered in Dify does not match the model_name defined in the proxy configuration, so the request names a model the proxy does not routeCopy the model alias exactly from the proxy configuration into the Dify model entry, including case and any prefix. Test the alias with a direct request to the proxy before retrying in Dify
Streaming responses cut offA reverse proxy, load balancer or the LiteLLM side has a read or idle timeout shorter than a long generation, so the connection closes mid-answerRaise the read and idle timeouts on every hop between Dify and the proxy, and confirm that buffering is disabled for streamed responses. Test a long streamed completion end to end
Embedding call failsAn alias configured for chat is used as the embedding model in a knowledge base, or the proxy route does not point to an embedding-capable modelAdd a separate alias for the embedding model, register it under the text embedding type in the plugin, and confirm the proxy route resolves to an embedding endpoint
Budget exceeded errorThe virtual key, user or team has reached its budget or its rate limit, which the proxy enforces before the provider is calledCheck the budget and limits on that key in the proxy, then either raise the budget through the proxy's temporary increase option or move the workload to a key with capacity. Set alerts so teams see the limit before users do
Spend or usage data missingPostgreSQL is not configured for the proxy, and virtual keys need a database for spend tracking, budgets and per-key recordsConfigure a PostgreSQL database for the proxy and confirm the proxy starts with it connected. Note that calls made before the database was attached are not recovered, so record the change date for audit purposes

Questions teams ask before connecting Dify to LiteLLM

What drives the effort of connecting Dify to a LiteLLM proxy?

The effort is driven mostly by how many providers sit behind the proxy, how many model aliases you define, and how much policy you attach to each key. Dify's LiteLLM plugin itself is configured with a base URL, a key and a model alias, so the Dify side is small. The larger share of work is on the gateway: provider accounts, alias naming, virtual key design, budgets, rate limits, fallback order and logging. Setting up PostgreSQL is also required once you use virtual keys, and Redis becomes necessary once more than one proxy instance runs. Most of this is one-time design and build. Recurring work includes rotating keys, reviewing spend, adjusting budgets and testing fallbacks after provider or model changes.

Can our team connect Dify to LiteLLM alone using the documentation?

A team with container and database experience can get a basic connection working from the Dify Marketplace plugin page and the LiteLLM docs. The harder part is the production layer: a proxy that stays up, a database and Redis configured for shared state, a secret manager holding the master key, virtual keys mapped to real owners, and routing that fails over correctly. Those choices need someone who owns the budget and the security review, not only someone who can paste a base URL. Ask yourself whether you have engineers who will maintain the gateway after launch, because the connection is only as reliable as the proxy behind it.

What does our organization need to provide before the work starts?

The work needs several inputs from your side before anyone configures a key. You need accounts and contracts with each model provider you plan to route to, along with a decision on which providers are approved for which data. Your network team must confirm that the Dify api and worker services can reach the proxy, and that the proxy can reach each provider endpoint or private model server. Name an owner for each virtual key and team, and a finance or platform owner who sets budgets and approves increases. Security should decide where the master key lives and who may read it.

How long does a LiteLLM connection take to set up?

No duration is fixed for this work, and the timeline depends mainly on the number of providers, the number of model aliases and the policy depth you require. A proxy with one provider and a few aliases is a much smaller job than one with several providers, private models, per-team budgets and SSO for the admin UI. Ask for the timeline in a written scope that lists each provider, each alias, the key and budget model, the fallback rules and the verification steps. Scope-based timelines are agreed in writing before work starts.

Does LiteLLM have to be an Enterprise license for this setup?

No. The core proxy is open source under the MIT license outside the enterprise directory, and the virtual keys, budgets, rate limits and routing that this integration depends on are part of the open-source proxy, with a PostgreSQL database for virtual keys. An Enterprise license becomes relevant for specific features. The admin UI SSO is free for up to 5 users from v1.76.0, and an Enterprise license is needed beyond that. SCIM, audit logs, organization and team RBAC, IP allowlisting and secret manager integrations are Enterprise features. Confirm the current feature split against the LiteLLM Enterprise documentation before you commit to a design.

Does Dify Enterprise change how LiteLLM is connected?

The connection method stays the same. The Dify LiteLLM plugin is configured with the proxy base URL, a key and a model alias whether you run Dify Community or Dify Enterprise. What changes is the platform around it. Dify Enterprise adds SSO, multiple workspaces and Kubernetes deployment for high availability, and its resources checklist lists Kubernetes, PostgreSQL, Redis, a Qdrant vector database and object storage as requirements. Those choices affect where the Dify services reach the proxy from and how workspaces receive model access, so plan network paths and workspace model settings together.

What should we ask for in a quote for this integration?

Ask for a written scope that lists each provider and model alias to be routed, the deployment target for the proxy and its database and Redis, and the key model with owners for master, team and virtual keys. Request the budget and rate limit design, the fallback and retry rules, the logging and spend tracking approach, the secret storage method, and the verification steps for each provider. Also ask which items are one-time and which recur, such as retainer support, upgrades and key reviews. Confirm who handles provider account setup and whether any Enterprise license cost falls outside the quote.

How DeployOpen scopes and delivers a Dify LiteLLM integration

DeployOpen begins every Dify and LiteLLM engagement with a written scope agreed before any work starts. That scope names the Dify workspaces and apps that will call models through the gateway, the LiteLLM proxy that will sit between them and the providers, the Dify LiteLLM plugin version that will be installed, and the environment where each piece runs. Because most engagements are fixed-scope, the deliverables and the acceptance checks are written down at this stage, so the client knows what is included before a quote is accepted. Confirm with DeployOpen which pieces are in scope, such as the proxy's database and Redis, the admin UI sign-in, and the Dify side configuration for each workspace.

The design work happens with the client before anything is deployed. DeployOpen plans the model alias map so that Dify apps reference stable names rather than provider model IDs, decides which team owns each virtual key, and agrees the budgets, TPM and RPM limits and model restrictions for each key, user or team. It also settles where the LITELLM_MASTER_KEY is stored and who may rotate it. These decisions shape how spend is reported and who is notified when a budget is reached, so they are better made once, in writing, than adjusted after the first invoice from a provider.

DeployOpen then deploys the proxy and configures the plugin inside the client's own cloud accounts, data centers or air-gapped network, and tests the result against the agreed checks, including streaming, tool calls, embedding models and fallback behavior when a provider is unavailable. The handover includes documentation of the alias map, key ownership and runbook steps. Ongoing work such as proxy and plugin upgrades, provider changes and budget reviews can be covered by a retainer. Ask for the upgrade and support terms in writing, and confirm them against your own change control process.

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.