客户端

getUser()

api.getUser() → Promise<User | null>

钥匙属于谁 平衡是什么 最短的电话能证明整个链条有效

GET /api/auth/mefreefour fields
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

返回值

{
  id: "…",                  // the account id
  email: "you@example.com",
  credits: 10869,           // the balance, at the moment of the call
  pack: "go"                // the plan this account is on
}
{
    "id": "…",                  # the account id
    "email": "you@example.com",
    "credits": 10869,           # the balance, at the moment of the call
    "pack": "go",               # the plan this account is on
}
idstring

账户。 稳定; 值得记录在任何你退帐的旁边。

emailstring

开户地址。

creditsinteger

平衡平衡,一种解读,而不是保留——见 下 数.

packstring

计划。 由决定配额,而不是决定模式价格。

服务器持有的比这多; 方法将其缩小到四个字段, 程序实际上会分支。 当您需要大记录时, 更新Profile () 返回该数值。

包包

值含义
空空计划开始一个账户。
上左付款计划
启动器一份付费计划, 有更多的一切。
企业由安排而不是自用安排决定。
freemiumAn older name that follows free.

阅读计划决定什么可以提供,从不计算价格:模型成本什么 (目录) 计划规定一个服务设施有多少房间——储存室、邮箱、主机——这些限制可以从执行这些限制的服务中读取。

当它为无效时

null 表示呼叫成功且没有记账。 它不是错误的密钥看起来像什么: 错误的密钥在会话交换之前就失败了 。

症状发生的事情
未经许可扔键被拒绝, 或者被撤销 。
无法到达的抛出基地没有回复,先检查地址
超时回答太慢了,或者根本不是
无效返回请求奏效了,但背后没有说明。

靴子检查

这是启动时要发出的呼吁。 它会运行密钥、 交换、 会话缓存和经认证的请求, 费用不高, 并且以四种不同的方式失败 。

import { IaGenify, IaGenifyError, Unauthorized } from "iagenify";

export async function checkIaGenify(api) {
  try {
    const user = await api.getUser();
    if (!user) return { ok: false, why: "no account behind this key" };
    return { ok: true, email: user.email, credits: user.credits };
  } catch (err) {
    if (err instanceof Unauthorized) return { ok: false, why: "key refused or revoked" };
    if (err instanceof IaGenifyError && err.code === "unreachable") {
      return { ok: false, why: `no answer at ${api.baseUrl}` };
    }
    throw err;
  }
}
from iagenify import IaGenifyError, Unauthorized

def check_iagenify(api):
    try:
        user = api.get_user()
        if not user:
            return {"ok": False, "why": "no account behind this key"}
        return {"ok": True, "email": user["email"], "credits": user["credits"]}
    except Unauthorized:
        return {"ok": False, "why": "key refused or revoked"}
    except IaGenifyError as error:
        if error.code == "unreachable":
            return {"ok": False, "why": f"no answer at {api.base_url}"}
        raise

读取平衡

当数字被读取时,数字是真实的,没有任何东西可以维持。 运行开始后,与账户上所有其他流程一样,在同样的余额下支出第二笔支出。

你不要使任何差事在他们的间道之间,

首先检查, 离开窗口后开支, 窗口成功和第二个成功都不应该有。 代之以给运行预算: 支出发生时强制执行, 而不是您所看的 。

// Fragile: two processes both read 400 and both start a 300-credit run
const { credits } = await api.getUser();
if (credits < 300) throw new Error("not enough");
await agent.run({ message });

// Sound: the ceiling travels with the run, and a shortfall is refused at the debit
const agent = api.agents.create({ runtime: "local", machine: pc, budget: 300 });
await agent.run({ message });
# Fragile: two processes both read 400 and both start a 300-credit run
if api.get_user()["credits"] < 300:
    raise RuntimeError("not enough")
agent.run(message=message)

# Sound: the ceiling travels with the run, and a shortfall is refused at the debit
agent = api.agents.create(runtime="local", machine=pc, model=model, budget=300)
agent.run(message=message)

亏空表面作为 NotEnoughCredits 例外,包括需要什么和存在什么——抓住而不是预测。 信贷信贷.

多久称呼它一次

每个电话都是往返的。 阅读一次在启动时, 然后再阅读一次, 然后再阅读一次支出,就足够了; 投票一次请求会增加你自己的终点的丁字裤, 并且告诉你在支出之间没有新情况。

let cached = null;
let readAt = 0;

export async function user(api, maxAgeMs = 60_000) {
  if (!cached || Date.now() - readAt > maxAgeMs) {
    cached = await api.getUser();
    readAt = Date.now();
  }
  return cached;
}
import time

_cached = None
_read_at = 0.0

def user(api, max_age=60):
    global _cached, _read_at
    if _cached is None or time.monotonic() - _read_at > max_age:
        _cached = api.get_user()
        _read_at = time.monotonic()
    return _cached