agentskit.js
Recipes

Recipe: Bail / Qwen routing

Use the bail (Alibaba DashScope) / qwen adapter alongside Western providers, with cost-aware routing for Asia-Pacific traffic.

The bail adapter (alias qwen) targets Alibaba's DashScope API, which hosts the Qwen family of models. Useful when you want native support for Chinese / Asia-Pacific compliance β€” DashScope's region selection means traffic stays in mainland China when needed.

#Install + basic call

import { bail } from '@agentskit/adapters'

const adapter = bail({
  apiKey: process.env.DASHSCOPE_API_KEY!,
  model: 'qwen-max',          // or qwen-plus, qwen-turbo, qwen-vl-*
  region: 'cn-beijing',       // or 'cn-hangzhou', 'singapore', etc.
})

qwen is an alias β€” same factory, same options:

import { qwen } from '@agentskit/adapters'

const adapter = qwen({ apiKey, model: 'qwen-max' })

#Routing: cheap default + premium fallback

Pair bail with createRouter to keep cost under control while still having a quality fallback:

import { bail, openai, createRouter } from '@agentskit/adapters'

const router = createRouter({
  candidates: [
    {
      id: 'qwen-turbo',
      adapter: bail({ apiKey: process.env.DASHSCOPE_API_KEY!, model: 'qwen-turbo' }),
      cost: 0.3,
      capabilities: { tools: true },
    },
    {
      id: 'qwen-max',
      adapter: bail({ apiKey: process.env.DASHSCOPE_API_KEY!, model: 'qwen-max' }),
      cost: 1.6,
      capabilities: { tools: true },
    },
    {
      id: 'gpt-4o',
      adapter: openai({ apiKey: process.env.OPENAI_API_KEY!, model: 'gpt-4o' }),
      cost: 2.5,
      capabilities: { tools: true },
      tags: ['western', 'fallback'],
    },
  ],
  // Default policy: 'cheapest' that satisfies the request.
  policy: 'cheapest',
  // Custom classify: route long-context English to gpt-4o.
  classify: req => {
    const lastUser = req.messages.findLast(m => m.role === 'user')
    if (lastUser?.content.length > 8_000) return 'gpt-4o'
    return undefined  // fall through to policy
  },
})

#Comparison snapshot

AdapterBest forRegionTool support
bail / qwenCN / APAC compliance, multilingual incl. ChineseDashScope (mainland China + Singapore)βœ…
openaiQuality + ecosystemUS (or Azure if you need EU)βœ…
anthropicLong context, tool useUSβœ…
geminiMultimodal + cost-effectiveGoogle globalβœ…
deepseekCoding-heavy + ultra-cheapHosted in CNβœ…

#Embedder pairing

Qwen also publishes text-embedding endpoints. Wire via the OpenAI-compatible factory:

import { createOpenAICompatibleEmbedder } from '@agentskit/adapters'

const qwenEmbedder = createOpenAICompatibleEmbedder({
  apiKey: process.env.DASHSCOPE_API_KEY!,
  model: 'text-embedding-v3',
  baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
})

Explore nearby

✎ Edit this page on GitHubΒ·Found a problem? Open an issue β†’Β·How to contribute β†’

On this page