opencode-litellm-cost 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +42 -0
- package/package.json +15 -0
- package/plugin.js +138 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Abraham
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# opencode-litellm-cost
|
|
2
|
+
|
|
3
|
+
An [opencode](https://opencode.ai) plugin that feeds real [LiteLLM](https://www.litellm.ai/) per-token pricing into opencode's model `cost` config, so cost tracking reflects what your LiteLLM proxy actually charges instead of opencode's built-in defaults.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
The plugin implements opencode's `config` hook. On config load, for every provider in your opencode config that has:
|
|
8
|
+
|
|
9
|
+
- an `options.baseURL` pointing at a LiteLLM proxy, and
|
|
10
|
+
- a matching entry in opencode's `auth.json` with `"type": "api"`,
|
|
11
|
+
|
|
12
|
+
it calls that proxy's `/model/info` endpoint, matches models by name against your configured `models` map, and writes `input`/`output`/`cache_read`/`cache_write` costs (and, where available, `context_over_200k` tiered pricing) onto each matching model's `cost` field.
|
|
13
|
+
|
|
14
|
+
It never throws — any failure (missing auth, unreachable proxy, malformed response) is caught and logged, and config loading continues normally.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
Add the plugin to your `opencode.json`/`opencode.jsonc`:
|
|
19
|
+
|
|
20
|
+
```jsonc
|
|
21
|
+
{
|
|
22
|
+
"plugin": ["opencode-litellm-cost"]
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or, if running from a local checkout, reference the file path directly:
|
|
27
|
+
|
|
28
|
+
```jsonc
|
|
29
|
+
{
|
|
30
|
+
"plugin": ["./plugins/opencode-litellm-cost/plugin.js"]
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Requirements
|
|
35
|
+
|
|
36
|
+
- A provider entry with `options.baseURL` set to your LiteLLM proxy base URL.
|
|
37
|
+
- An entry for that same provider ID in `~/.local/share/opencode/auth.json` (or `$XDG_DATA_HOME/opencode/auth.json`) with `"type": "api"` and a valid `key`.
|
|
38
|
+
- Model keys in your provider's `models` map that match the `model_name` values LiteLLM reports from `/model/info`.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT — see [LICENSE](./LICENSE).
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-litellm-cost",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Feeds real LiteLLM per-token pricing into opencode's model cost config",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "plugin.js",
|
|
7
|
+
"author": "Kevin Abraham <kevin@westhousefarm.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/abraha2d/opencode-litellm-cost.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/abraha2d/opencode-litellm-cost#readme",
|
|
14
|
+
"private": false
|
|
15
|
+
}
|
package/plugin.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* opencode plugin: feed real LiteLLM per-token pricing into model cost config.
|
|
7
|
+
* Only implements the `config` hook. Never throws.
|
|
8
|
+
*/
|
|
9
|
+
export default async function litellmCostPlugin() {
|
|
10
|
+
return {
|
|
11
|
+
config: async (cfg) => {
|
|
12
|
+
try {
|
|
13
|
+
const dataDir =
|
|
14
|
+
process.env.XDG_DATA_HOME ||
|
|
15
|
+
path.join(os.homedir(), ".local", "share");
|
|
16
|
+
const authPath = path.join(dataDir, "opencode", "auth.json");
|
|
17
|
+
|
|
18
|
+
if (!fs.existsSync(authPath)) return;
|
|
19
|
+
|
|
20
|
+
let auth;
|
|
21
|
+
try {
|
|
22
|
+
auth = JSON.parse(fs.readFileSync(authPath, "utf8"));
|
|
23
|
+
} catch {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const updateProvider = async (providerID, providerCfg) => {
|
|
28
|
+
try {
|
|
29
|
+
if (!providerCfg?.options?.baseURL) return;
|
|
30
|
+
if (
|
|
31
|
+
!providerCfg?.models ||
|
|
32
|
+
typeof providerCfg.models !== "object" ||
|
|
33
|
+
Object.keys(providerCfg.models).length === 0
|
|
34
|
+
)
|
|
35
|
+
return;
|
|
36
|
+
|
|
37
|
+
const providerAuth = auth?.[providerID];
|
|
38
|
+
if (
|
|
39
|
+
!providerAuth ||
|
|
40
|
+
providerAuth.type !== "api" ||
|
|
41
|
+
typeof providerAuth.key !== "string"
|
|
42
|
+
)
|
|
43
|
+
return;
|
|
44
|
+
|
|
45
|
+
const base = providerCfg.options.baseURL.replace(/\/+$/, "");
|
|
46
|
+
const url = base + "/model/info";
|
|
47
|
+
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
50
|
+
let res;
|
|
51
|
+
try {
|
|
52
|
+
res = await fetch(url, {
|
|
53
|
+
headers: { Authorization: `Bearer ${providerAuth.key}` },
|
|
54
|
+
signal: controller.signal,
|
|
55
|
+
});
|
|
56
|
+
} finally {
|
|
57
|
+
clearTimeout(timeout);
|
|
58
|
+
}
|
|
59
|
+
if (!res.ok) return;
|
|
60
|
+
|
|
61
|
+
const body = await res.json();
|
|
62
|
+
const infoList = Array.isArray(body?.data) ? body.data : [];
|
|
63
|
+
const priceByName = new Map(
|
|
64
|
+
infoList.map((m) => [m.model_name, m.model_info ?? {}]),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
for (const [modelKey, modelCfg] of Object.entries(
|
|
68
|
+
providerCfg.models,
|
|
69
|
+
)) {
|
|
70
|
+
const info = priceByName.get(modelKey);
|
|
71
|
+
if (!info) continue;
|
|
72
|
+
|
|
73
|
+
const input = info.input_cost_per_token;
|
|
74
|
+
const output = info.output_cost_per_token;
|
|
75
|
+
if (typeof input !== "number" || typeof output !== "number")
|
|
76
|
+
continue;
|
|
77
|
+
|
|
78
|
+
const cost = {
|
|
79
|
+
input: input * 1_000_000,
|
|
80
|
+
output: output * 1_000_000,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
if (typeof info.cache_read_input_token_cost === "number")
|
|
84
|
+
cost.cache_read = info.cache_read_input_token_cost * 1_000_000;
|
|
85
|
+
if (typeof info.cache_creation_input_token_cost === "number")
|
|
86
|
+
cost.cache_write =
|
|
87
|
+
info.cache_creation_input_token_cost * 1_000_000;
|
|
88
|
+
|
|
89
|
+
const over200kInput = info.input_cost_per_token_above_200k_tokens;
|
|
90
|
+
const over200kOutput =
|
|
91
|
+
info.output_cost_per_token_above_200k_tokens;
|
|
92
|
+
if (
|
|
93
|
+
typeof over200kInput === "number" &&
|
|
94
|
+
typeof over200kOutput === "number"
|
|
95
|
+
) {
|
|
96
|
+
const over = {
|
|
97
|
+
input: over200kInput * 1_000_000,
|
|
98
|
+
output: over200kOutput * 1_000_000,
|
|
99
|
+
};
|
|
100
|
+
if (
|
|
101
|
+
typeof info.cache_read_input_token_cost_above_200k_tokens ===
|
|
102
|
+
"number"
|
|
103
|
+
)
|
|
104
|
+
over.cache_read =
|
|
105
|
+
info.cache_read_input_token_cost_above_200k_tokens *
|
|
106
|
+
1_000_000;
|
|
107
|
+
if (
|
|
108
|
+
typeof info.cache_creation_input_token_cost_above_200k_tokens ===
|
|
109
|
+
"number"
|
|
110
|
+
)
|
|
111
|
+
over.cache_write =
|
|
112
|
+
info.cache_creation_input_token_cost_above_200k_tokens *
|
|
113
|
+
1_000_000;
|
|
114
|
+
cost.context_over_200k = over;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
modelCfg.cost = cost;
|
|
118
|
+
}
|
|
119
|
+
} catch (err) {
|
|
120
|
+
console.error(
|
|
121
|
+
`litellm-cost: provider ${providerID} failed: ${
|
|
122
|
+
err?.name ?? "Error"
|
|
123
|
+
}: ${err?.message ?? String(err)}`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
await Promise.allSettled(
|
|
129
|
+
Object.entries(cfg.provider ?? {}).map(([providerID, providerCfg]) =>
|
|
130
|
+
updateProvider(providerID, providerCfg),
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
console.error("litellm-cost: config hook failed:", err);
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|