Ruby SDK

Use Nemo Router with the ruby-openai gem

Last updated

Nemo Router works with the ruby-openai gem. Point uri_base at https://api.nemorouter.ai/v1 and pass your sk-nemo-... key — guardrails, caching, and rate-limits auto-apply from your org config.

Installation

gem install ruby-openai

Setup

require "openai"

client = OpenAI::Client.new(
  access_token: ENV["NEMOROUTER_API_KEY"],
  uri_base: "https://api.nemorouter.ai/v1",
)

Chat Completion

require "openai"

client = OpenAI::Client.new(
  access_token: ENV["NEMOROUTER_API_KEY"],
  uri_base: "https://api.nemorouter.ai/v1",
)

response = client.chat(
  parameters: {
    model: "claude-sonnet-4-20250514",
    messages: [{ role: "user", content: "Hello!" }],
  }
)
puts response.dig("choices", 0, "message", "content")

Per-Request Overrides

ruby-openai forwards unknown parameters, so nemo_* fields work inline:

# Apply only specific guardrails
response = client.chat(
  parameters: {
    model: "gpt-4o",
    messages: [{ role: "user", content: "Summarize Q1 earnings..." }],
    nemo_guardrail_ids: ["guardrail-uuid-1", "guardrail-uuid-2"],
  }
)

# Run a prompt template with variables
response = client.chat(
  parameters: {
    model: "gpt-4o",
    messages: [{ role: "user", content: "Summarize Q1 earnings..." }],
    nemo_prompt_template_id: "your-summarizer-id",
    nemo_prompt_variables: { language: "Spanish", max_length: "100" },
  }
)

# Bypass cache for a single call
response = client.chat(
  parameters: {
    model: "gpt-4o",
    messages: [{ role: "user", content: "What is the latest news?" }],
    nemo_cache: false,
  }
)

Error Handling

begin
  client.chat(
    parameters: {
      model: "gpt-4o",
      messages: [{ role: "user", content: "My SSN is 123-45-6789" }],
    }
  )
rescue => e
  # "guardrail_blocked" — guardrail rejected the request
  # "insufficient_credits" — top up to continue
  puts "Error: #{e.message}"
end

Response Headers

Every successful response carries:

  • x-nemo-request-cost — exact USD spend for this call
  • x-nemo-guardrails-applied — comma-separated guardrail names that ran
  • x-nemo-prompt-version — version of the prompt template applied (if any)

Next Steps

Was this page helpful?