- GET /api/dashboard → owner-only contextcost.read bridge (plugins/hubcontextcost.py) - /dashboards becomes DashboardsHome, a card grid of the dashboard plugins' pages - one-time carry-over of the stat-card order/bench into the plugin's layout.json - telemetry: /dashboards is dash.home; the plugin page keeps dash.overview Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
"""``contextcost.read`` — what the Claude context costs to load (since
|
||
2026-11-28, the ``context-skills`` plugin: the core's *Context & skills*
|
||
dashboard, ``/dashboards`` and ``GET /api/dashboard``, left the core).
|
||
|
||
One owner-only bridge, operator approval:
|
||
|
||
* ``GET /proxy/tools/plugins/{pid}/hub/context-cost`` → the old
|
||
``/api/dashboard`` answer, unchanged: ``pricing`` (the token counter's model
|
||
and its $ per 1M input tokens), ``totals`` (files, tokens, cost, how many
|
||
counts are still estimated, skills, invocations, the skills' estimated
|
||
spend), ``skills`` (per skill: calls, last used, the tokens / $ of loading
|
||
its ``.claude/skills/<name>/`` dir once, ``estSpend`` = that × calls, and
|
||
``path`` — its ``SKILL.md`` when the file index has it, for a link) and
|
||
``topFiles`` (the 12 most expensive context files).
|
||
|
||
Nothing here walks the disk or a transcript: it reads the indexer's ``files``
|
||
and ``skills`` tables (``deps.store``), the same as the route did. A guest —
|
||
or a plugin without the capability — gets a 403 ``capability``.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from fastapi import APIRouter, Request
|
||
from fastapi.responses import JSONResponse
|
||
from starlette.concurrency import run_in_threadpool
|
||
|
||
import schemas
|
||
from core.http import _r
|
||
from plugins.proxyroutes import _owner_bridge
|
||
|
||
router = APIRouter()
|
||
|
||
CAP = "contextcost.read"
|
||
TOP_FILES = 12
|
||
|
||
|
||
def build(deps) -> dict:
|
||
"""The Context & skills figures off the indexer's tables."""
|
||
from dashboards.routes import _context_by_skill
|
||
from indexer import INPUT_PRICE_PER_MTOK, MODEL
|
||
|
||
files = deps.store.all_files()
|
||
total_tokens = sum((r["tokens"] or 0) for r in files.values())
|
||
total_cost = sum((r["cost"] or 0.0) for r in files.values())
|
||
estimated = sum(1 for r in files.values() if r["estimated"])
|
||
by_skill = _context_by_skill(files)
|
||
|
||
skills = []
|
||
for row in deps.store.all_skills():
|
||
ctx = by_skill.get(row["name"])
|
||
ctx_cost = ctx["cost"] if ctx else None
|
||
skill_md = f".claude/skills/{row['name']}/SKILL.md"
|
||
skills.append({
|
||
"name": row["name"],
|
||
"count": row["count"],
|
||
"lastUsed": row["last_used"],
|
||
"contextTokens": ctx["tokens"] if ctx else None,
|
||
"contextCost": ctx_cost,
|
||
# estimated total spend = cost of loading the skill once × times used
|
||
"estSpend": (ctx_cost * row["count"]) if ctx_cost is not None else None,
|
||
"path": skill_md if skill_md in files else None,
|
||
})
|
||
|
||
top_files = sorted(
|
||
({"path": p, "tokens": r["tokens"] or 0, "cost": r["cost"] or 0.0,
|
||
"estimated": bool(r["estimated"])}
|
||
for p, r in files.items()),
|
||
key=lambda f: f["cost"], reverse=True,
|
||
)[:TOP_FILES]
|
||
|
||
return {
|
||
"pricing": {"model": MODEL, "inputPerMTok": INPUT_PRICE_PER_MTOK},
|
||
"totals": {
|
||
"files": len(files),
|
||
"tokens": total_tokens,
|
||
"cost": total_cost,
|
||
"estimated": estimated,
|
||
"skills": len(skills),
|
||
"invocations": sum(s["count"] for s in skills),
|
||
"skillSpend": sum(s["estSpend"] for s in skills if s["estSpend"] is not None),
|
||
},
|
||
"skills": skills,
|
||
"topFiles": top_files,
|
||
}
|
||
|
||
|
||
@router.get("/proxy/tools/plugins/{pid}/hub/context-cost", responses=_r(schemas.Dashboard))
|
||
async def plugin_hub_context_cost(pid: str, request: Request):
|
||
"""``contextcost.read``: the token cost of the context files, the skills'
|
||
usage and estimated spend, the most expensive files. Owner only."""
|
||
deps, err = _owner_bridge(request, pid, CAP)
|
||
if err:
|
||
return err
|
||
return JSONResponse(await run_in_threadpool(build, deps))
|