Orqen Docs

Examples

Python tool-calling agent

A complete Python loop using the OpenAI SDK with Orqen as the base URL.

agent.py
from openai import OpenAI
import json
import os

client = OpenAI(
    api_key=os.environ["ORQEN_API_KEY"],
    base_url="https://api.orqen.app/v1",
)

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "list_workspace_files",
            "description": "List files in the workspace. Use when the user asks to list files, folders, or directory contents.",
            "parameters": {
                "type": "object",
                "properties": {"subdir": {"type": "string", "default": ""}},
                "required": [],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a city. Use when the user asks about weather, forecast, rain, or temperature.",
            "x-orqen-examples": ["weather in London", "is it raining in Paris"],
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"],
            },
        },
    },
]

def call_tool(name: str, args: dict) -> dict:
    if name == "list_workspace_files":
        return {"entries": ["README.md", "src", "package.json"]}
    if name == "get_weather":
        return {"city": args["city"], "temperature_c": 13, "conditions": "light rain"}
    return {"error": f"Unknown tool: {name}"}

def run(user_message: str) -> str:
    messages = [{"role": "user", "content": user_message}]

    for _ in range(8):
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=messages,
            tools=TOOLS,
            tool_choice="auto",
        )
        message = response.choices[0].message
        if not message.tool_calls:
            return message.content or ""

        messages.append(message)
        for call in message.tool_calls:
            args = json.loads(call.function.arguments or "{}")
            result = call_tool(call.function.name, args)
            messages.append({
                "role": "tool",
                "tool_call_id": call.id,
                "content": json.dumps(result),
            })

    return "Stopped after max tool rounds."

print(run("What is the weather in London?"))

Inspect pruning headers

import httpx

response = httpx.post(
    "https://api.orqen.app/v1/chat/completions",
    headers={"Authorization": "Bearer sk-orq-YOUR_KEY"},
    json={
        "model": "gpt-4o",
        "messages": [{"role": "user", "content": "list files"}],
        "tools": TOOLS,
    },
)

print(response.headers.get("x-orqen-tools-input"))
print(response.headers.get("x-orqen-tools-output"))
print(response.headers.get("x-orqen-routing"))