mchisolm0M
Convex Community7mo ago
20 replies
mchisolm0

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")
})

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)
YouTubeConvex
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 ...
Powerful AI Apps Made Easy with the Agent Component
OpenRouter
The unified interface for LLMs. Find the best models & prices for your prompts
OpenRouter
Was this page helpful?