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.
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
idfield. Required and cannot be empty.
Options
- API Key - OpenRouter API key credential (optional if using Connection Id).
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 identifiertotal_cost- Total cost in USDcreated_at- Timestamp when generation was createdmodel- Model used for generationorigin- Origin of the requestusage- Usage amountis_byok- Whether BYOK (Bring Your Own Key) was usedupstream_id- Upstream provider's IDcache_discount- Cache discount amountupstream_inference_cost- Cost charged by upstream providerapp_id- Application IDstreamed- Whether response was streamedcancelled- Whether request was cancelledprovider_name- Provider name (e.g., "OpenAI", "Anthropic")latency- Total latency in millisecondsmoderation_latency- Moderation latency in millisecondsgeneration_time- Time spent generating in millisecondsfinish_reason- Reason generation finishednative_finish_reason- Provider's original finish reasontokens_prompt- Tokens in prompttokens_completion- Tokens in completionnative_tokens_prompt- Provider's prompt token countnative_tokens_completion- Provider's completion token countnative_tokens_reasoning- Reasoning tokens (for reasoning models)num_media_prompt- Number of media items in promptnum_media_completion- Number of media items in completionnum_search_results- Number of search results used
How It Works
When executed, the node:
- Validates the connection or credentials
- Ensures Generation Id is not empty
- Blocks execution if using Robomotion AI Credits (not supported)
- Builds the API request URL with generation ID as query parameter
- Makes GET request to
/generation?id={generation_id} - Parses the response and extracts generation metadata
- 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 costupstream_inference_cost: What OpenRouter paid the providercache_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
-
Enable Raw Responses:
- Always enable "Include Raw Response" on generation nodes when you need metadata
- Extract and store generation IDs for later analysis
-
Cost Monitoring:
- Track costs for production deployments
- Set up budget alerts based on cumulative costs
- Monitor cost trends over time
-
Performance Tracking:
- Log latency and generation time for SLA monitoring
- Compare provider performance
- Identify slow queries for optimization
-
Token Management:
- Monitor token usage to stay within limits
- Track token efficiency (output tokens per input token)
- Use token counts for cost prediction
-
Provider Analysis:
- Compare upstream costs across providers
- Identify best value providers for your use cases
- Monitor provider reliability (finish_reason, cancelled)
-
Caching Strategy:
- Monitor cache discount to validate caching effectiveness
- Adjust prompts to maximize cache hits
- Track cache savings over time
-
Audit & Compliance:
- Maintain generation logs for audit requirements
- Track model usage for compliance
- Record costs for chargeback or billing
-
Error Analysis:
- Check finish_reason for quality issues
- Monitor cancellation rates
- Track failed generations
Use Cases for RPA
- Cost Allocation: Track AI costs per department or project
- Performance SLAs: Monitor response times for customer-facing applications
- Budget Management: Enforce spending limits on AI operations
- Provider Optimization: Choose most cost-effective providers
- Usage Analytics: Understand token consumption patterns
- Audit Trails: Maintain records for compliance and billing
- Quality Monitoring: Track finish reasons and completion rates
- Cache Optimization: Measure and improve prompt caching effectiveness