在 AI 应用开发中,选择合适的 LLM API 服务至关重要。无论你是在构建智能对话系统、开发 AI Agent,还是参加 AI Hackathon,本文都将为你提供全面的 API 使用指南,涵盖 OpenRouter、Anthropic API、火山引擎和 Siliconflow 等主流服务。

为什么需要多个 API 服务?

不同的 LLM 模型有各自的优势,特别是在开发 AI Agent 时,需要根据具体场景选择合适的模型:

  • Claude(Anthropic):擅长复杂推理、编程和 Agent 任务,特别适合需要深度思考的场景
  • Gemini(Google):在长文本处理、多模态理解方面表现出色,适合处理图片、视频等多媒体内容
  • GPT(OpenAI):图片理解和数学推理能力强,日常对话体验优秀
  • 豆包(字节跳动):国内访问速度快,语音对话体验好,特别适合实时交互场景
  • 开源模型:成本低,可定制性强,适合大规模部署

OpenRouter:一站式访问所有模型

OpenRouter 是我最推荐的服务,它提供了统一的 API 接口来访问各种模型,无需担心地域限制。对于 AI Agent 开发者来说,这意味着可以轻松切换和组合不同的模型。

优势

  1. 无地域限制:可以直接从国内访问各种模型
  2. 统一接口:使用 OpenAI 格式的 API,简化编程
  3. 模型丰富:支持 Claude、Gemini、GPT、Grok 等主流模型
  4. 便于 Agent 开发:可以在一个系统中灵活调用不同能力的模型

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from openai import OpenAI

client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="<OPENROUTER_API_KEY>",
)

# 使用 Claude Sonnet 进行复杂推理(适合 Agent 的深度思考)
completion = client.chat.completions.create(
model="anthropic/claude-sonnet-4",
messages=[
{
"role": "user",
"content": "请解释什么是递归算法,并给出一个 Python 示例"
}
]
)
print(completion.choices[0].message.content)

# 多模态示例:图片理解(适合需要视觉能力的 Agent)
completion = client.chat.completions.create(
model="anthropic/claude-sonnet-4",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
}
}
]
}
]
)
print(completion.choices[0].message.content)

AI Agent 开发的模型选择策略

在开发 AI Agent 时,不同的任务需要不同的模型:

  • 编程和 Agent 核心逻辑(工具调用)anthropic/claude-sonnet-4
  • 长文本分析、报告生成、多模态google/gemini-2.5-pro
  • 实时响应(快思考)google/gemini-2.5-flash
  • 日常对话openai/gpt-4o

Anthropic 官方 API

虽然 OpenRouter 很方便,但某些场景下还是需要使用官方 API,比如使用 Claude Code 或 Computer Use 功能。

注意事项

⚠️ 重要:Anthropic API key 不要在国内和香港 IP 使用,需要使用海外 IP 访问。

使用场景

  • Claude Code
  • Computer Use(让 Agent 操作计算机)

⚠️ 特殊功能提示:Claude 在 Agent 开发中的工具调用(Tool Use)和思考模式(Thinking Mode)需要特殊的语法格式。请参考 Anthropic 官方文档 获取最新的使用方法。

火山引擎:豆包模型

火山引擎提供的豆包模型在国内访问延迟低,特别适合对响应速度要求高的实时 Agent 应用。

AI Agent 开发的模型选择

火山引擎提供了三种不同能力的豆包模型,适合不同的 AI Agent 场景:

1. 快慢思考架构

在开发 “快慢思考” 架构的 Agent 时:

快思考模型(低延迟)- doubao-seed-1-6-flash-250615

1
2
3
4
5
6
7
8
9
10
response = client.chat.completions.create(
model="doubao-seed-1-6-flash-250615",
messages=[
{
"role": "user",
"content": "今天天气怎么样?"
}
],
stream=True # 流式输出,减少延迟
)

慢思考模型(深度推理)- doubao-seed-1-6-thinking-250615

1
2
3
4
5
6
7
8
9
10
response = client.chat.completions.create(
model="doubao-seed-1-6-thinking-250615",
messages=[
{
"role": "user",
"content": "请详细解释相对论的基本原理"
}
],
stream=True # 流式输出中间思考过程
)

2. 多模态 Agent

适合需要处理图片的 Agent - doubao-seed-1-6-250615

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
from openai import OpenAI

client = OpenAI(
base_url="https://ark.cn-beijing.volces.com/api/v3",
api_key=os.environ.get("ARK_API_KEY"),
)

response = client.chat.completions.create(
model="doubao-seed-1-6-250615",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://ark-project.tos-cn-beijing.ivolces.com/images/view.jpeg"
},
},
{"type": "text", "text": "这是哪里?"},
],
}
],
)

print(response.choices[0])

Siliconflow:开源模型的最佳选择

Siliconflow 为 AI Agent 开发提供了完整的模型生态,包括 LLM、TTS(文本转语音)和 ASR(语音识别)。

LLM 模型

推荐用于 Agent 开发的模型:

  • Kimi K2 Instructmoonshotai/Kimi-K2-Instruct
  • DeepSeek R1 0528deepseek-ai/DeepSeek-R1
  • Qwen3 235BQwen/Qwen3-235B-A22B-Instruct-2507
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests

url = "https://api.siliconflow.cn/v1/chat/completions"

payload = {
"model": "moonshotai/Kimi-K2-Instruct",
"messages": [
{
"role": "user",
"content": "What opportunities and challenges will the Chinese large model industry face in 2025?"
}
],
"stream": True # Agent 开发推荐使用流式输出
}
headers = {
"Authorization": "Bearer <SILICONFLOW_API_KEY>",
"Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers, stream=True)
# 处理流式响应...

语音 Agent 开发套件

TTS 文本转语音

使用 Fish Audio 或 CosyVoice 进行语音合成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import requests
import os

url = "https://api.siliconflow.cn/v1/audio/speech"

payload = {
"model": "fishaudio/fish-speech-1.5",
"input": "Nice to meet you!",
"voice": "fishaudio/fish-speech-1.5:alex",
"response_format": "mp3",
"speed": 1.0
}
headers = {
"Authorization": "Bearer " + os.environ['SILICONFLOW_API_KEY'],
"Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers)

# 保存音频文件
with open("output.mp3", "wb") as f:
f.write(response.content)

ASR 语音识别

使用 SenseVoice 进行语音识别:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from openai import OpenAI
import os

client = OpenAI(
base_url="https://api.siliconflow.cn/v1",
api_key=os.environ['SILICONFLOW_API_KEY'],
)

def transcribe_audio(speech_file_path):
with open(speech_file_path, "rb") as audio_file:
transcription = client.audio.transcriptions.create(
model="FunAudioLLM/SenseVoiceSmall",
file=audio_file,
language="zh", # 指定语言可以提高准确率
prompt="这是一段对话录音" # 提供上下文
)
return transcription.text

# 在实时语音 Agent 中的使用示例
def process_audio_chunk(audio_chunk):
# 1. 使用 VAD 检测语音结束(推荐 Silero VAD)
# 2. 调用 ASR 识别
text = transcribe_audio(audio_chunk)
# 3. 传递给 LLM 处理
return text

使用 AI 辅助开发 Agent

在开发 AI Agent 时,推荐使用 Cursor 等 AI 辅助编程工具,实践 “用 Agent 开发 Agent”:

  1. 文档先行:先让 AI 生成设计文档,迭代优化后再编码
  2. 选对模型:使用带思考能力的模型(如 Claude 4 Sonnet)
  3. 测试驱动:让 AI 为代码编写测试用例

AI 代码编辑器选择

除了 Cursor、Windsurf、Trae 这些需要氪金的 AI 代码编辑器,你还可以使用 OpenRouter API key 在其他编辑器中获得类似体验:

  • Void AI 编辑器:支持直接使用 OpenRouter API key,功能与 Cursor 类似
  • VSCode Cline 插件:可以配置 OpenRouter API key,在 VSCode 中实现 AI 辅助编程

使用 OpenRouter 的优势是可以在同一个 API key 下切换不同的模型,找到最适合你编程风格的 AI 助手。

总结

选择合适的 LLM API 服务是构建优秀 AI Agent 的关键:

  1. 地域限制:优先使用 OpenRouter 避免访问问题
  2. 任务需求:根据 Agent 的具体功能选择擅长的模型
  3. 延迟要求:实时交互选择低延迟模型,深度思考选择能力强的模型
  4. 成本考虑:平衡性能和成本,合理使用开源模型
  5. 架构设计:采用混合模型架构,发挥不同模型的优势

快速参考:常用模型列表

用途 推荐模型 特点
编程/Agent 开发 anthropic/claude-sonnet-4 代码能力强,工具调用稳定
深度推理 anthropic/claude-sonnet-4 支持思考过程输出
快速响应 google/gemini-2.5-flash 延迟极低,适合实时交互
长文本处理 google/gemini-2.5-pro 上下文窗口大,理解能力强
平衡性能 openai/gpt-4o 各方面能力均衡
成本优化 google/gemini-2.5-flash 性价比高
国内低延迟 doubao-seed-1-6-flash-250615 国内访问快

通过合理组合使用这些 API 服务,你可以构建出功能强大、响应迅速的 AI Agent。记住,优秀的 Agent 不是依赖单一模型,而是懂得在合适的场景使用合适的工具。

祝你在 AI Agent 开发之旅中取得成功!

Comments