-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplarix-measured.yml
More file actions
166 lines (150 loc) · 5.69 KB
/
Copy pathplarix-measured.yml
File metadata and controls
166 lines (150 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Plarix Measured Mode Example
#
# This workflow demonstrates "measured mode" - the most accurate way to track
# LLM costs by using actual token usage from your CI tests.
#
# How it works:
# 1. Checkout the BASE commit (target branch)
# 2. Run your test suite that produces a usage log (JSONL format)
# 3. Checkout the HEAD commit (PR branch)
# 4. Run the same test suite
# 5. Plarix compares actual token usage between BASE and HEAD
#
# Your test suite must output JSONL with this format:
# {"provider":"openai","model":"gpt-4o","input_tokens":1500,"output_tokens":500}
# {"provider":"anthropic","model":"claude-sonnet-4","input_tokens":2000,"output_tokens":800}
name: LLM Cost Analysis (Measured)
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
measure-costs:
runs-on: ubuntu-latest
steps:
- name: Checkout BASE (target branch)
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
fetch-depth: 0
- name: Setup environment
# Add your language/runtime setup here
# Example for Python:
# uses: actions/setup-python@v5
# with:
# python-version: '3.12'
run: echo "Setting up environment..."
- name: Install dependencies (BASE)
run: |
# Install your project dependencies
# Example: pip install -r requirements.txt
echo "Installing dependencies..."
- name: Run tests and measure BASE usage
run: |
# Run your test suite that produces LLM usage logs
# The test must output JSONL to a file
#
# Example for a Python project:
# python -m pytest tests/ --llm-usage-log=base_usage.jsonl
#
# Or run your custom measurement script:
# ./scripts/measure-llm-usage.sh > base_usage.jsonl
# For this example, we create a placeholder
echo '{"provider":"openai","model":"gpt-4o","input_tokens":1000,"output_tokens":200}' > base_usage.jsonl
echo '{"provider":"anthropic","model":"claude-sonnet-4","input_tokens":500,"output_tokens":100}' >> base_usage.jsonl
- name: Save BASE usage artifact
uses: actions/upload-artifact@v4
with:
name: base-usage
path: base_usage.jsonl
retention-days: 1
- name: Checkout HEAD (PR branch)
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
clean: false
- name: Install dependencies (HEAD)
run: |
# Reinstall in case dependencies changed
echo "Installing dependencies..."
- name: Run tests and measure HEAD usage
run: |
# Same test suite as BASE
# Example:
# python -m pytest tests/ --llm-usage-log=head_usage.jsonl
# For this example, we create a placeholder with different values
echo '{"provider":"openai","model":"gpt-4o","input_tokens":1200,"output_tokens":250}' > head_usage.jsonl
echo '{"provider":"anthropic","model":"claude-sonnet-4","input_tokens":600,"output_tokens":120}' >> head_usage.jsonl
- name: Download BASE usage artifact
uses: actions/download-artifact@v4
with:
name: base-usage
path: .
- name: Run Plarix (Measured Mode)
uses: aegix-ai/plarix-action@v0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
env:
PLARIX_MEASURE_BASE: base_usage.jsonl
PLARIX_MEASURE_HEAD: head_usage.jsonl
# =============================================================================
# JSONL Format Reference
# =============================================================================
#
# Each line in your usage log should be valid JSON with these fields:
#
# Required fields:
# - provider: "openai" or "anthropic"
# - model: Model identifier (e.g., "gpt-4o", "claude-sonnet-4")
# - input_tokens: Number of input/prompt tokens
# - output_tokens: Number of output/completion tokens
#
# Optional fields:
# - cached_input_tokens: Tokens served from cache (Anthropic prompt caching)
# - timestamp: ISO 8601 timestamp of the API call
#
# Example lines:
# {"provider":"openai","model":"gpt-4o","input_tokens":1500,"output_tokens":500}
# {"provider":"anthropic","model":"claude-sonnet-4","input_tokens":2000,"output_tokens":800,"cached_input_tokens":500}
# {"provider":"openai","model":"o1","input_tokens":3000,"output_tokens":1000,"timestamp":"2025-01-15T10:30:00Z"}
#
# =============================================================================
# Generating Usage Logs
# =============================================================================
#
# Option 1: Wrap your LLM client
# Create a wrapper around your OpenAI/Anthropic client that logs each call
#
# Option 2: Use a proxy
# Route API calls through a logging proxy that captures usage
#
# Option 3: Parse API responses
# Most LLM APIs return token usage in the response - log that data
#
# Example Python wrapper:
#
# import json
# from openai import OpenAI
#
# def log_usage(provider, model, input_tokens, output_tokens, log_file="usage.jsonl"):
# with open(log_file, "a") as f:
# f.write(json.dumps({
# "provider": provider,
# "model": model,
# "input_tokens": input_tokens,
# "output_tokens": output_tokens
# }) + "\n")
#
# client = OpenAI()
# response = client.chat.completions.create(
# model="gpt-4o",
# messages=[{"role": "user", "content": "Hello!"}]
# )
# log_usage(
# "openai",
# response.model,
# response.usage.prompt_tokens,
# response.usage.completion_tokens
# )