Skip to main content

Get Generation

Retrieves generation metadata including cost, token usage, and performance metrics from OpenRouter for a specific generation ID.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • Connection Id - The connection identifier from Connect node (optional if API Key is provided directly).
  • Generation Id - Generation ID from a previous API response. This is found in the raw response's id field. Required and cannot be empty.

Options

  • API Key - OpenRouter API key credential (optional if using Connection Id).
warning

This node does NOT support "Use Robomotion AI Credits" mode. You must use your own OpenRouter API key to access generation metadata.

Output

  • Generation - Generation metadata object containing:
    • id - Generation identifier
    • total_cost - Total cost in USD
    • created_at - Timestamp when generation was created
    • model - Model used for generation
    • origin - Origin of the request
    • usage - Usage amount
    • is_byok - Whether BYOK (Bring Your Own Key) was used
    • upstream_id - Upstream provider's ID
    • cache_discount - Cache discount amount
    • upstream_inference_cost - Cost charged by upstream provider
    • app_id - Application ID
    • streamed - Whether response was streamed
    • cancelled - Whether request was cancelled
    • provider_name - Provider name (e.g., "OpenAI", "Anthropic")
    • latency - Total latency in milliseconds
    • moderation_latency - Moderation latency in milliseconds
    • generation_time - Time spent generating in milliseconds
    • finish_reason - Reason generation finished
    • native_finish_reason - Provider's original finish reason
    • tokens_prompt - Tokens in prompt
    • tokens_completion - Tokens in completion
    • native_tokens_prompt - Provider's prompt token count
    • native_tokens_completion - Provider's completion token count
    • native_tokens_reasoning - Reasoning tokens (for reasoning models)
    • num_media_prompt - Number of media items in prompt
    • num_media_completion - Number of media items in completion
    • num_search_results - Number of search results used

How It Works

When executed, the node:

  1. Validates the connection or credentials
  2. Ensures Generation Id is not empty
  3. Blocks execution if using Robomotion AI Credits (not supported)
  4. Builds the API request URL with generation ID as query parameter
  5. Makes GET request to /generation?id={generation_id}
  6. Parses the response and extracts generation metadata
  7. Returns the complete generation data object

Requirements

  • Valid OpenRouter API Key (Robomotion AI Credits NOT supported)
  • Valid Generation Id from a previous API call
  • The generation must exist in your OpenRouter account

Error Handling

The node will return specific errors in the following cases:

  • Empty or missing Generation Id
  • Using Robomotion AI Credits mode (unsupported)
  • Invalid Generation Id (404)
  • API authentication errors (401)
  • API service errors (500, 502, 503, 504)

Usage Notes

Why Track Generation Metadata?

  • Cost Tracking: Monitor exact costs per generation
  • Performance Analysis: Measure latency and generation time
  • Token Usage: Track prompt and completion tokens for budgeting
  • Provider Comparison: Compare costs and performance across providers
  • Audit Trail: Maintain records of AI operations
  • Optimization: Identify expensive queries for optimization

Getting Generation IDs

Generation IDs are found in the raw API responses from:

  • Generate Text node (enable "Include Raw Response")
  • Chat Completion node (enable "Include Raw Response")
  • Generate Image node (enable "Include Raw Response")

Extract the id field from the response: msg.rawResponse.id

Cost Analysis

The generation metadata provides detailed cost breakdown:

  • total_cost: Your total cost
  • upstream_inference_cost: What OpenRouter paid the provider
  • cache_discount: Savings from prompt caching
  • OpenRouter's margin is visible from the difference

Token Tracking

Two sets of token counts are provided:

  • OpenRouter counts (tokens_prompt, tokens_completion): Normalized counts
  • Native counts (native_tokens_prompt, native_tokens_completion): Provider's raw counts
  • Use native counts for precise tracking with specific providers

Examples

Example 1: Track Generation Cost

Step 1: Generate Text with Raw Response

// Generate Text Node
// Options: Include Raw Response = true
// Output: msg.response, msg.rawResponse

msg.generation_id = msg.rawResponse.id;

Step 2: Get Generation Metadata

// Get Generation Node
// Input: msg.generation_id
// Output: msg.generation

console.log(`Cost: $${msg.generation.total_cost}`);
console.log(`Tokens: ${msg.generation.tokens_prompt + msg.generation.tokens_completion}`);

Example 2: Performance Monitoring

Input:

  • Generation Id: msg.generation_id

Output Analysis:

console.log(`Model: ${msg.generation.model}`);
console.log(`Total Latency: ${msg.generation.latency}ms`);
console.log(`Generation Time: ${msg.generation.generation_time}ms`);
console.log(`Provider: ${msg.generation.provider_name}`);

Example 3: Cost Comparison Across Models

// Generate with Model A
// Get Generation -> msg.cost_a = msg.generation.total_cost

// Generate with Model B
// Get Generation -> msg.cost_b = msg.generation.total_cost

// Compare
if (msg.cost_a < msg.cost_b) {
console.log(`Model A is ${((msg.cost_b - msg.cost_a) / msg.cost_b * 100).toFixed(1)}% cheaper`);
}

Example 4: Token Usage Budget Tracking

msg.total_tokens_used = msg.total_tokens_used || 0;
msg.total_cost = msg.total_cost || 0;

// After each generation
msg.total_tokens_used += msg.generation.tokens_prompt + msg.generation.tokens_completion;
msg.total_cost += msg.generation.total_cost;

// Budget check
const TOKEN_BUDGET = 1000000;
const COST_BUDGET = 10.0;

if (msg.total_tokens_used > TOKEN_BUDGET || msg.total_cost > COST_BUDGET) {
throw new Error("Budget exceeded!");
}

Example 5: Audit Log Creation

msg.audit_logs = msg.audit_logs || [];

msg.audit_logs.push({
timestamp: new Date().toISOString(),
generation_id: msg.generation.id,
model: msg.generation.model,
provider: msg.generation.provider_name,
cost: msg.generation.total_cost,
tokens: {
prompt: msg.generation.tokens_prompt,
completion: msg.generation.tokens_completion,
total: msg.generation.tokens_prompt + msg.generation.tokens_completion
},
latency: msg.generation.latency,
finish_reason: msg.generation.finish_reason
});

// Save to database or file

Example 6: Cache Effectiveness Analysis

if (msg.generation.cache_discount > 0) {
const savings_percent = (msg.generation.cache_discount / msg.generation.total_cost) * 100;
console.log(`Cache saved ${savings_percent.toFixed(1)}% ($${msg.generation.cache_discount})`);
}

Best Practices

  1. Enable Raw Responses:

    • Always enable "Include Raw Response" on generation nodes when you need metadata
    • Extract and store generation IDs for later analysis
  2. Cost Monitoring:

    • Track costs for production deployments
    • Set up budget alerts based on cumulative costs
    • Monitor cost trends over time
  3. Performance Tracking:

    • Log latency and generation time for SLA monitoring
    • Compare provider performance
    • Identify slow queries for optimization
  4. Token Management:

    • Monitor token usage to stay within limits
    • Track token efficiency (output tokens per input token)
    • Use token counts for cost prediction
  5. Provider Analysis:

    • Compare upstream costs across providers
    • Identify best value providers for your use cases
    • Monitor provider reliability (finish_reason, cancelled)
  6. Caching Strategy:

    • Monitor cache discount to validate caching effectiveness
    • Adjust prompts to maximize cache hits
    • Track cache savings over time
  7. Audit & Compliance:

    • Maintain generation logs for audit requirements
    • Track model usage for compliance
    • Record costs for chargeback or billing
  8. Error Analysis:

    • Check finish_reason for quality issues
    • Monitor cancellation rates
    • Track failed generations

Use Cases for RPA

  1. Cost Allocation: Track AI costs per department or project
  2. Performance SLAs: Monitor response times for customer-facing applications
  3. Budget Management: Enforce spending limits on AI operations
  4. Provider Optimization: Choose most cost-effective providers
  5. Usage Analytics: Understand token consumption patterns
  6. Audit Trails: Maintain records for compliance and billing
  7. Quality Monitoring: Track finish reasons and completion rates
  8. Cache Optimization: Measure and improve prompt caching effectiveness