openrouter-audit-logging

Featured

Implement audit logging for OpenRouter API calls. Use when building compliance trails, debugging production issues, or tracking model usage. Triggers: 'openrouter audit', 'openrouter logging', 'audit trail openrouter', 'log openrouter requests'.

AI & Automation 2,940 stars 439 forks Updated today MIT

Install

View on GitHub

Quality Score: 99/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# OpenRouter Audit Logging ## Overview Every OpenRouter API call returns a generation ID and metadata that enables comprehensive audit logging. The generation endpoint (`GET /api/v1/generation?id=`) provides exact cost, token counts, provider used, and latency -- data that the initial response doesn't always include. This skill covers structured logging, cost tracking, PII redaction, and compliance-ready audit trails. ## Core: Generation Metadata Retrieval ```python import os, json, time, hashlib, logging from datetime import datetime, timezone from dataclasses import dataclass, asdict from typing import Optional import requests from openai import OpenAI log = logging.getLogger("openrouter.audit") @dataclass class AuditEntry: timestamp: str generation_id: str model_requested: str model_used: str # Actual model served (may differ with fallbacks) prompt_tokens: int completion_tokens: int total_cost: float latency_ms: float status: str # "success" | "error" | "timeout" user_id: str prompt_hash: str # SHA-256 of prompt (not raw content) error_code: Optional[str] = None client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], default_headers={ "HTTP-Referer": "https://my-app.com", "X-Title": "my-app", }, ) def audited_completion( messages: list[dict], model: str = "anthropic/claude-3.5-sonnet", user_id: str = "...

Details

Author
jeremylongshore
Repository
jeremylongshore/claude-code-plugins-plus-skills
Created
1 years ago
Last Updated
today
Language
Python
License
MIT

Integrates with

Bundled in these plugins

Similar Skills

Semantically similar based on skill content — not just same category