New to Kestra?
Use blueprints to kickstart your first workflows.
Read the OpenRouter credit balance every 3 hours, call no model, and alert Slack before agents and flows fail because prepaid credits ran out.
Many agents, RAG pipelines and AI flows reach dozens of models through OpenRouter on prepaid credits. When the balance hits zero, every call returns a payment-required error at once: the support bot stops answering, the nightly summarization job fails, and the first sign is a wave of errors. OpenRouter shows the balance on its credits page, but nothing pages the team as it runs down. This blueprint turns a scheduled, read-only call to the OpenRouter credits endpoint into a spend gate: io.kestra.plugin.core.http.Request reads credits purchased and used, one jq query works out the remaining balance, and the result routes to a quiet log or a Slack alert.
get_credits (io.kestra.plugin.core.http.Request) calls GET /api/v1/credits with a bearer management key. It is a read: no model is called and nothing is purchased. Retries cover short network blips, and allowFailed plus allowFailure keep an API error from aborting the flow so it can be reported instead.summarize_credits (io.kestra.plugin.core.debug.Return) first guards for a missing or non-200 response (UNREACHABLE; OpenRouter answers 403 if the key is not a management key). Otherwise one jq query computes data.total_credits - data.total_usage and the share used, and classifies: a remaining balance at or below critical_remaining_usd is CRITICAL, at or below warning_remaining_usd is WARNING, otherwise HEALTHY.route_by_credits (io.kestra.plugin.core.flow.Switch) branches on that classification: HEALTHY logs quietly, WARNING posts a Slack note, CRITICAL and UNREACHABLE page Slack. A defaults fallback logs anything unexpected.log_audit_result (io.kestra.plugin.core.log.Log) writes one audit line per run with the HTTP status and the full summary.errors block pages Slack if the flow itself fails, and the credit_check trigger runs every 3 hours, shipped disabled: true.status and remaining_usd outputs other flows can gate on, for example to pause expensive batch jobs or switch an agent to a cheaper model when credits are low.The flows that spend the credits often already run in Kestra. Kestra turns a plain, read-only API call into a scheduled, typed classification with retries, a failure path and an execution history, and the remaining_usd output lets those same flows throttle themselves before the balance runs out.
OPENROUTER_MANAGEMENT_KEY: an OpenRouter management key.SLACK_WEBHOOK: Slack incoming webhook used for every alert branch and the flow-failure alert.In Kestra OSS, secrets are supplied as environment variables prefixed SECRET_ and base64-encoded, for example SECRET_OPENROUTER_MANAGEMENT_KEY=$(echo -n 'sk-or-v1-xxxxxxxx' | base64), and read back with {{ secret('OPENROUTER_MANAGEMENT_KEY') }}. Use the Enterprise secrets backend for encryption at rest and access control.
OPENROUTER_MANAGEMENT_KEY and SLACK_WEBHOOK as secrets.remaining_usd with the balance on the OpenRouter credits page.warning_remaining_usd above your current balance to see the WARNING path, then set it back.credit_check.total_usage in a KV store to compute the daily burn and alert when the balance will run out within N days, not only when it is low.status == HEALTHY with a Flow trigger.warning_remaining_usd: type FLOAT, default 20.0.critical_remaining_usd: type FLOAT, default 5.0.status: one of HEALTHY, WARNING, CRITICAL, UNREACHABLE.remaining_usd: remaining credit balance in USD.UNREACHABLE with that hint.