Skip to content

[openrouter] Add flex service tier for models with flex endpoints - #4136

Open
visul wants to merge 1 commit into
langgenius:mainfrom
visul:feat/openrouter-service-tier-flex
Open

visul wants to merge 1 commit into
langgenius:mainfrom
visul:feat/openrouter-service-tier-flex

Conversation

@visul

@visul visul commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Summary

OpenRouter now accepts service_tier on Chat Completions (Service Tiers docs). With service_tier: "flex" it tries the model's flex endpoints first, which are billed at a lower rate (for example openai/gpt-6-luna flex is listed at 0.05 / 0.25 per 1M tokens vs 0.10 / 0.50 standard). The native OpenAI and Gemini plugins already expose a Flex tier (#2981); the OpenRouter plugin had no way to request it.

This PR adds a Service Tier parameter (auto | flex, default auto):

  • Predefined models: added to the 28 models in the catalog that have at least one flex endpoint on OpenRouter, checked with GET https://openrouter.ai/api/v1/models/{model}/endpoints (endpoint tags ending in /flex):
    • OpenAI (openai/flex): gpt-5-mini, gpt-5-nano, gpt-5.1, gpt-5.6-*, gpt-6-*, gpt-6.1-*
    • Google (google-ai-studio/flex, google-vertex/global/flex): gemini-2.5-*, gemini-3-flash-preview, gemini-3.1-*, gemini-3.5-flash-lite, gemini-3.7-flash, gemini-3.8-flash
    • DeepInfra flex: deepseek-chat, gemma-4-31b-it, glm-5.2, minimax-m3
  • Custom models: the same rule is added in get_customizable_model_schema. A model without a flex endpoint routes normally at standard rates, so the option is safe there.
  • Request: _set_service_tier_params sends service_tier: "flex" only when flex is selected. auto, empty or unknown values leave the parameter out, so default routing is unchanged.

The help text states OpenRouter's fallback rule: flex only falls back to endpoints priced at or below the flex endpoint, otherwise a capacity error is returned. Without a flex endpoint the request routes normally.

Known limitation: usage cost shown in Dify still comes from the standard pricing in each YAML, so flex savings appear on the OpenRouter invoice but not in Dify's cost numbers. This matches how the plugin already handles per-provider price differences on OpenRouter; the help text says so.

Release Notes

Add a Service Tier parameter (auto / flex) to OpenRouter models with flex endpoints and to custom models, forwarding service_tier: "flex" so requests can use OpenRouter's lower-cost flex endpoints.

Change Type

  • Documentation / non-plugin change
  • Non-LLM plugin (tools, extensions, datasource, etc.)
  • LLM plugin

LLM Plugin Checklist

Areas affected by this change (check all that apply)
  • Message flow (system messages, user ↔ assistant turn-taking)
  • Tool interaction flow (multi-round usage, Agent App and Agent Node)
  • Multimodal input (images, PDFs, audio, video, etc.)
  • Multimodal output (images, audio, video, etc.)
  • Structured output (JSON, XML, etc.)
  • Token consumption metrics
  • Other LLM functionality (reasoning, grounding, prompt caching, etc.)
  • New models / model parameter fixes

Version

  • Bumped top-level version in manifest.yaml (0.1.12 → 0.1.13)
  • dify_plugin dependency unchanged

Testing

  • Offline unit tests (pytest: 39 passed): flex forwarded, auto / empty / unknown values dropped, YAML rules consistent, custom model schema exposes the rule, _invoke sends service_tier next to the existing provider.data_collection
  • Live call through the plugin's _invoke against the OpenRouter API (openai/gpt-6-luna): with flex the response reported service_tier: "flex", with auto it reported default
  • Live API call with google/gemini-3.8-flash and service_tier: "flex": served by Google AI Studio on the flex tier
  • Packaged with dify plugin package (v0.6.11)

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant