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
| Adapter | Best for | Region | Tool support |
|---|---|---|---|
bail / qwen | CN / APAC compliance, multilingual incl. Chinese | DashScope (mainland China + Singapore) | β |
openai | Quality + ecosystem | US (or Azure if you need EU) | β |
anthropic | Long context, tool use | US | β |
gemini | Multimodal + cost-effective | Google global | β |
deepseek | Coding-heavy + ultra-cheap | Hosted 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',
})#Related
Explore nearby
- PeerRecipes
Copy-paste solutions grouped by theme. Every recipe end-to-end, runs as written.
- PeerCustom adapter
Wrap any LLM API as an AgentsKit adapter. Plug-and-play with the rest of the kit in 30 lines.
- PeerAdapter contract tests
Verify any adapter against the ADR 0001 invariants A1βA10 with the shared test harness.
Recipe: Scaffolding with @agentskit/templates
Generate a publishable AgentsKit package β tool, skill, adapter, embedder, memory, browser-adapter, or flow β in a few lines of Node.
Chat with RAG
A streaming React chat that answers from your own documents. Vector store, embeddings, retrieval, hooked up in 30 lines.