APIKO
開發框架

Strands Agents

在 Python 或 TypeScript 中以 APIKO 對話模型執行 Strands Agents

概覽

Strands Agents 是以 Python 與 TypeScript 建構 AI 智慧代理的開源 SDK。它內建的 OpenAI 模型提供者可以直接用於 APIKO:指向閘道,再使用目錄中任意對話模型 ID 即可,包括 Claude、GPT、Gemini 與 DeepSeek 模型。工具呼叫的用法與直連 OpenAI 時相同。

先決條件

  • Python 3.10 或更新版本,或 Node.js 22 或更新版本
  • 從 主控台 → API Keys 取得的 APIKO API Key,並儲存到環境變數 APIKO_API_KEY

第 1 步:安裝 Strands Agents

bash
pip install 'strands-agents[openai]'

第 2 步:讓 Strands 指向 APIKO

python
import os

from strands import Agent
from strands.models.openai import OpenAIModel

model = OpenAIModel(
    client_args={
        "api_key": os.environ["APIKO_API_KEY"],
        "base_url": "https://api.apiko.app/v1",
    },
    model_id="gpt-6.1-sol",
)

agent = Agent(model=model)
result = agent("Reply with the word ok")
print(result.metrics.accumulated_usage)

將 gpt-6.1-sol 替換為 GET /v1/models 為你的 Key 回傳的任意對話模型 ID。

第 3 步:驗證設定

執行腳本,應看到 ok 以及 Token 數量;該請求也會出現在 主控台 → 任務日誌。401 表示 Key 錯誤,403 通常表示餘額不足,404 model_not_found 表示該模型 ID 對你的 Key 不可用。

工具呼叫

工具照常傳給智慧代理。模型決定何時呼叫,Strands 會執行工具並在同一輪迴圈裡把結果送回模型。

python
from strands import Agent, tool

@tool
def word_count(text: str) -> int:
    """Count the words in a piece of text."""
    return len(text.split())

agent = Agent(model=model, tools=[word_count])
agent("How many words are in 'the quick brown fox jumps'? Use the word_count tool.")

注意事項

  • Strands 預設使用串流輸出,接入 APIKO 不需要額外設定。
  • 使用 deepseek-v4-pro 等推理模型時,Strands 在 Chat Completions 的多輪對話中不會回傳先前的推理內容,並會輸出一則警告;回答與工具呼叫不受影響。
  • 圖片與影片模型以非同步任務方式執行,請依 任務介面 呼叫,而不是使用對話模型提供者。
  • 已於 2026-11-12 使用 strands-agents 1.64.0(Python)與 @strands-agents/sdk 1.24.0(TypeScript)對 APIKO 閘道實測通過,模型為 gpt-6.1-sol、deepseek-v4-pro 與 gemini-3.8-flash,包括工具呼叫。