Агенты: учёт токенов (agent_usage, буфер в памяти + пачки в БД), globals с лимитами аккаунта, доллары убраны
Расчётная стоимость из ответов claude не является списанием (тариф Team, 0 из 10 USD), поэтому результаты, API и записи больше её не ведут. Токены каждого вызова модели - в agent_usage; проценты 5-часового и недельного окон читаются у claude в globals (одна строка, check), предупреждение ИТ на 80% и 95%, сводка GET /agent/usage. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,14 +18,19 @@ import { DriveInbox } from "./slack/drive-inbox.js";
|
||||
import { SlackIntake, rowToManager } from "./slack/intake.js";
|
||||
import { TodoEventPoller } from "./slack/todo-events.js";
|
||||
import { createGoogleTts } from "./slack/tts.js";
|
||||
import { LimitsPoller, UsageLog } from "./usage.js";
|
||||
|
||||
const { common, service } = loadServiceSettings("khbuild_assistant_prep", assistantPrepSchema);
|
||||
const log = createLogger("khbuild_assistant_prep", common.logLevel);
|
||||
const pool = createPool(common.database.url);
|
||||
|
||||
const usageLog = new UsageLog(pool, log);
|
||||
usageLog.start();
|
||||
|
||||
const ctx: AgentContext = {
|
||||
pool,
|
||||
log,
|
||||
usageLog,
|
||||
settings: service.agent,
|
||||
runner: createCliRunner({ bin: service.agent.claudeBin, timeoutMs: service.agent.timeoutMs, model: service.agent.model }),
|
||||
liveRunner: service.agent.live?.managerIds.length
|
||||
@@ -91,6 +96,7 @@ const chat = chatDeps
|
||||
|
||||
const app = buildApi({
|
||||
pool,
|
||||
usageLog,
|
||||
token: service.http.token,
|
||||
chat,
|
||||
run: (job, opts) =>
|
||||
@@ -130,8 +136,14 @@ if (service.slack && chatDeps) {
|
||||
}
|
||||
}
|
||||
|
||||
// Лимиты аккаунта агентов (проценты окон -> globals, предупреждение ИТ на 80% и 95%) и чистка старых записей учёта токенов (раз в сутки, храним 180 дней)
|
||||
const limitsPollMinutes = service.agent.limits?.pollMinutes ?? 10;
|
||||
if (limitsPollMinutes > 0)
|
||||
new LimitsPoller(pool, log, ctx.notifyIt, { intervalMs: limitsPollMinutes * 60_000, credentialsPath: service.agent.limits?.credentialsPath }).start();
|
||||
setInterval(() => void usageLog.prune(180).catch((e) => log.warn({ err: String(e) }, "чистка agent_usage не удалась")), 24 * 3600_000).unref();
|
||||
|
||||
for (const sig of ["SIGTERM", "SIGINT"] as const)
|
||||
process.on(sig, () => {
|
||||
ctx.liveRunner?.closeAll();
|
||||
process.exit(0);
|
||||
void usageLog.stop().finally(() => process.exit(0));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user