mchisolm0
mchisolm02mo ago

Agent Component with OpenRouter

Goal: Add Agent Component to my web and React Native app with Expo Subgoal: I want the user to be able to switch models easily (hence OpenRouter) I had this working with Convex without the Agent Component, but I wanted to switch to using the Agent Component. 1. Is there no need for an API key, even for OpenAI integration? I do not see in the docs or video tutorials (like here https://www.youtube.com/watch?v=tUKMPUlOCHY&t=308s) where an API key is used. I imagine it is in an environment variable, but I just did not see it in the docs or video. 2. Is OpenRouter not supported? It looks like the Agent Component expects LanguageModelV1 for chat and createOpenRouter({apiKey: ...}) gives a type of OpenRouterChatLanguageModel which is apparently v2. Here is the current code in my convex directory and the error on chat
import { Agent } from "@convex-dev/agent"
import { createOpenRouter } from "@openrouter/ai-sdk-provider"
import { components } from "./_generated/api"
import { LanguageModelV1 } from "ai"

const openrouter = createOpenRouter({
apiKey: process.env.OPENROUTER_API_KEY,
baseURL: process.env.OPENROUTER_BASE_URL,
})

const chatAgent = new Agent(components.agent, {
name: "chat-agent",
chat: openrouter.chat("google/gemini-2.0-flash-001")
})
import { Agent } from "@convex-dev/agent"
import { createOpenRouter } from "@openrouter/ai-sdk-provider"
import { components } from "./_generated/api"
import { LanguageModelV1 } from "ai"

const openrouter = createOpenRouter({
apiKey: process.env.OPENROUTER_API_KEY,
baseURL: process.env.OPENROUTER_BASE_URL,
})

const chatAgent = new Agent(components.agent, {
name: "chat-agent",
chat: openrouter.chat("google/gemini-2.0-flash-001")
})
process.env.OPENROUTER_BASE_URL is https://openrouter.ai/api/v1 Below is the actual type error
Type 'OpenRouterChatLanguageModel' is not assignable to type 'LanguageModelV1'.
Types of property 'specificationVersion' are incompatible.
Type '"v2"' is not assignable to type '"v1"'.ts(2322)
Type 'OpenRouterChatLanguageModel' is not assignable to type 'LanguageModelV1'.
Types of property 'specificationVersion' are incompatible.
Type '"v2"' is not assignable to type '"v1"'.ts(2322)
Convex
YouTube
Powerful AI Apps Made Easy with the Agent Component
If you’re a developer looking to build full-stack AI apps without dealing with messy orchestration, this video is your shortcut. Learn how to integrate the open-source Agent Component from Convex into your project to build real-time, intelligent chat interfaces powered by LLMs like GPT-4.1 mini. All in TypeScript. Resources - Agent component ...
OpenRouter
OpenRouter
The unified interface for LLMs. Find the best models & prices for your prompts
11 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ampp
ampp2mo ago
you just call openrouter(model) and make sure the api key is set on convex. its actually quite simple
mchisolm0
mchisolm0OP2mo ago
That is amazing. Going to go try it now. I will take that simplicity even if it means I spent 2 hours unnecessarily debugging. Is there any expectation of what the key should be called on convex? OPENROUTER_API_KEY is what I suspect
ampp
ampp2mo ago
yep https://discordapp.com/channels/1019350475847499849/1384983022729826376 i was inspired by the getModel setup here and made like a slight modification for my agent init
mchisolm0
mchisolm0OP2mo ago
Where do you import openrouter from to call openrouter(model)?
ampp
ampp2mo ago
import { openrouter } from '@openrouter/ai-sdk-provider' you might need to call openrouter.chat( depending
mchisolm0
mchisolm0OP2mo ago
Okay, then based on what you were saying, I was doing it the right way all along. When I do that I get the error
Type 'OpenRouterChatLanguageModel' is not assignable to type 'LanguageModelV1'.
Types of property 'specificationVersion' are incompatible.
Type '"v2"' is not assignable to type '"v1"'.ts(2322)
Type 'OpenRouterChatLanguageModel' is not assignable to type 'LanguageModelV1'.
Types of property 'specificationVersion' are incompatible.
Type '"v2"' is not assignable to type '"v1"'.ts(2322)
When I dug into it, I think OpenRouter is structuring it with v2 of LanguageModels but the Convex Agent Component expects v1.
mchisolm0
mchisolm0OP2mo ago
No description
mchisolm0
mchisolm0OP2mo ago
I get the same error with or without .chat( @ampp do you prefer I ping you if I ask something new? I did confirm that the Convex examples show that it should just work like you said, ampp. I have reached out to the OpenRouter team. I am also going to check what version of the @openrouter/ai-sdk-provider is in the example repo. I may just install that version. Just to document it, I can confirm the Agent Component works with @openrouter/ai-sdk-provider@0.7.1. I will update if the OpenRouter team updates me with the latest version that still uses v1. Also, would the Convex team be open to help in upgrading the Agent Component to v2? I am open to working on it, but I wanted to avoid assuming the help was wanted. If it is wanted, please let me know anything you would like me to be mindful of other than what is in the CONTRIBUTING.md.
ampp
ampp2mo ago
there is the #agents chat here if you aren't aware
mchisolm0
mchisolm0OP2mo ago
Thank you! No I was not aware

Did you find this page helpful?