Openrouter

Learn how to use Openrouter with Composio

Overview

SLUG: OPENROUTER

Description

OpenRouter is a platform that provides a unified API for accessing various large language models (LLMs) from different providers, allowing developers to integrate multiple AI models seamlessly.

Authentication Details

generic_api_key
stringRequired

Connecting to Openrouter

Create an auth config

Use the dashboard to create an auth config for the Openrouter toolkit. This allows you to connect multiple Openrouter accounts to Composio for agents to use.

1

Select App

Navigate to Openrouter.

2

Configure Auth Config Settings

Select among the supported auth schemes of and configure them here.

3

Create and Get auth config ID

Click “Create Openrouter Auth Config”. After creation, copy the displayed ID starting with ac_. This is your auth config ID. This is not a sensitive ID — you can save it in environment variables or a database. This ID will be used to create connections to the toolkit for a given user.

Connect Your Account

Using API Key

1from composio import Composio
2
3# Replace these with your actual values
4openrouter_auth_config_id = "ac_YOUR_OPENROUTER_CONFIG_ID" # Auth config ID created above
5user_id = "0000-0000-0000" # UUID from database/app
6
7composio = Composio()
8
9def authenticate_toolkit(user_id: str, auth_config_id: str):
10 # Replace this with a method to retrieve an API key from the user.
11 # Or supply your own.
12 user_api_key = input("[!] Enter API key")
13
14 connection_request = composio.connected_accounts.initiate(
15 user_id=user_id,
16 auth_config_id=auth_config_id,
17 config={"auth_scheme": "API_KEY", "val": user_api_key}
18 )
19
20 # API Key authentication is immediate - no redirect needed
21 print(f"Successfully connected Openrouter for user {user_id}")
22 print(f"Connection status: {connection_request.status}")
23
24 return connection_request.id
25
26
27connection_id = authenticate_toolkit(user_id, openrouter_auth_config_id)
28
29# You can verify the connection using:
30connected_account = composio.connected_accounts.get(connection_id)
31print(f"Connected account: {connected_account}")

Tools

Executing tools

To prototype you can execute some tools to see the responses and working on the Openrouter toolkit’s playground

Python
1from composio import Composio
2from openai import OpenAI
3import json
4
5openai = OpenAI()
6composio = Composio()
7
8# User ID must be a valid UUID format
9user_id = "0000-0000-0000" # Replace with actual user UUID from your database
10
11tools = composio.tools.get(user_id=user_id, toolkits=["OPENROUTER"])
12
13print("[!] Tools:")
14print(json.dumps(tools))
15
16def invoke_llm(task = "What can you do?"):
17 completion = openai.chat.completions.create(
18 model="gpt-4o",
19 messages=[
20 {
21 "role": "user",
22 "content": task, # Your task here!
23 },
24 ],
25 tools=tools,
26 )
27
28 # Handle Result from tool call
29 result = composio.provider.handle_tool_calls(user_id=user_id, response=completion)
30 print(f"[!] Completion: {completion}")
31 print(f"[!] Tool call result: {result}")
32
33invoke_llm()

Tool List

Tool Name: Create Chat Completion

Description

Tool to generate a chat-style completion. use after assembling messages and selecting a model. supports streaming and function calls.

Action Parameters

frequency_penalty
number
function_call
string
functions
array
logit_bias
object
max_tokens
integer
messages
arrayRequired
model
stringRequired
n
integer
presence_penalty
number
stop
string
stream
boolean
temperature
number
top_p
number
user
string

Action Response

data
objectRequired
error
string
successful
booleanRequired

Tool Name: Create Completion

Description

Tool to generate a text completion for a given prompt or set of messages. use when you need a model-generated response from a specified model.

Action Parameters

max_tokens
integer
messages
array
model
stringRequired
prompt
string
stop
string
stream
boolean
temperature
number
top_k
integer
top_p
number

Action Response

data
objectRequired
error
string
successful
booleanRequired

Tool Name: Get Credits

Description

Tool to get the current api credit balance for the authenticated user. use after authenticating to monitor remaining credits before making further api calls.

Action Parameters

Action Response

data
objectRequired
error
string
successful
booleanRequired

Tool Name: Get Generation

Description

Tool to retrieve a generation result by its unique id. use after a generation completes to fetch metadata like token counts, cost, and latency.

Action Parameters

id
stringRequired

Action Response

data
objectRequired
error
string
successful
booleanRequired

Tool Name: List Available Models

Description

Tool to list available models via openrouter api. use after confirming authentication to fetch the model catalog.

Action Parameters

category
string
use_rss
boolean
use_rss_chat_links
boolean

Action Response

data
arrayRequired
error
string
successful
booleanRequired

Tool Name: OpenRouter List Model Endpoints

Description

Tool to list endpoints for a specific model. use after specifying model author and slug to get endpoint details including pricing, context length, and supported parameters.

Action Parameters

author
stringRequired
slug
stringRequired

Action Response

data
objectRequired
error
string
successful
booleanRequired

Tool Name: OpenRouter List Providers

Description

Tool to list all ai model providers available through the openrouter api. use after authentication to retrieve available provider options for routing configuration.

Action Parameters

Action Response

data
arrayRequired
error
string
successful
booleanRequired