Skip to content

Cost-Optimized Routing

Example showing how to use Martian's router parameters for cost-optimized model selection.

This example assumes using Martian; is in scope and apiKey contains your Martian API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
using var client = new MartianClient(apiKey);

// Use the router with cost constraints to optimize model selection.
// The models parameter restricts which models the router can choose from.
// willingness_to_pay controls cost vs. quality trade-off.
var response = await client.CreateChatCompletionAsync(
    model: "openai/gpt-4.1-nano",
    messages: [new ChatCompletionMessage
    {
        Role = ChatCompletionMessageRole.User,
        Content = "Explain the concept of machine learning in one sentence.",
    }],
    models: ["openai/gpt-4.1-nano", "openai/gpt-4.1-mini"],
    maxCost: 0.01f,
    willingnessToPay: 0.1f);