Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Last active July 1, 2026 04:30
Show Gist options
  • Select an option

  • Save kuc-arc-f/ea1412cd8f1619facab0f405d6d8b5c3 to your computer and use it in GitHub Desktop.

Select an option

Save kuc-arc-f/ea1412cd8f1619facab0f405d6d8b5c3 to your computer and use it in GitHub Desktop.
OpenRouter AI SDK , tool example

OpenRouter AI SDK , tool example


  • install
npm install @openrouter/ai-sdk-provider ai zod tsx

  • start
npx tsx example.ts

import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { generateText, tool, stepCountIs } from 'ai';
import { z } from 'zod';
const OPENROUTER_API_KEY="your-key";
const MODEL_NAME = "openai/gpt-oss-120b";
export const getWeather = async (modelName: string) => {
const openrouter = createOpenRouter({
apiKey: OPENROUTER_API_KEY,
});
const { text } = await generateText({
model: openrouter(modelName),
prompt: 'Tokyo の天気は ?',
tools: {
getWeather: tool({
description: 'Get the current weather in a given city.',
parameters: z.object({
city: z.string().describe('The city name'),
}),
execute: async ({ city }) => {
return `The weather in ${city} is sunny.`;
},
}),
},
stopWhen: stepCountIs(5),
});
console.log(text);
return text;
};
const main = async function(){
await getWeather(MODEL_NAME);
}
main();
{
"name": "ai_test1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@openrouter/ai-sdk-provider": "^2.10.0",
"ai": "^6.0.217",
"tsx": "^4.22.4",
"zod": "^4.4.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment