跳到内容
Runic
中文
Esc
导航打开⌘J预览
本页内容

快速入门

在几分钟内将 askRunic 和 storeResult 接入现有智能体。

安装

npm install @runic-labs/sdk
pnpm add @runic-labs/sdk
yarn add @runic-labs/sdk
bun add @runic-labs/sdk

@runic-labs/sdk 依赖于 @runic-labs/cache@runic-labs/ledger——除非你想自行构建自定义存储实例,否则无需单独安装它们(参见 缓存账本)。

包装智能体现有的调用

找到智能体中将任务转换为 LLM 或工具调用的位置,并使用 askRunic / storeResult 将其包装起来:

import { askRunic, storeResult } from "@runic-labs/sdk";

async function reviewFile(repo: string, file: string) {
  const decision = { intent: "review_code", params: { repo, file } };

  const cached = await askRunic(decision);
  if (cached) {
    console.log(`cache hit — saved ${cached.tokensSpent} tokens`);
    return cached.artifact;
  }

  const response = await callYourLLM(promptFor(repo, file));
  await storeResult(decision, response.text, response.tokensUsed);
  return response.text;
}

集成就这么简单。无需任何配置即可开始使用——askRunic/storeResult 会在当前工作目录下的 .runic/ 中使用默认的文件支持存储。

选择 intentparams

签名采用精确匹配,因此请根据你的使用场景决定什么条件会让两次调用成为“同一决策”:

  • 良好{ intent: "summarize_pr", params: { repo: "acme/widgets", pr: 482 } }——同一仓库、同一 PR 编号、同一意图;每次查询这个确切的 PR 时都保持一致。
  • 不佳{ intent: "summarize_pr", params: { prompt: fullPromptString } }——将原始提示词文本作为参数,意味着任何措辞改动(哪怕只是空白字符)都会导致缓存未命中,从而失去意义。

只将真正用于标识决策的参数放入 params。将提示词文本、推理轨迹以及任何非确定性内容完全排除在签名之外——原因请参见工作原理

查看已缓存的内容

npm install -g @runic-labs/cli
pnpm add -g @runic-labs/cli
npm install -g @runic-labs/cli
bun add -g @runic-labs/cli
runic cache status
runic ledger status

完整输出示例请参见 CLI

指向不同的存储位置

默认情况下,Runic 会写入 process.cwd() 中的 .runic/。如果智能体运行所在的工作目录不同于你希望存放状态的位置,可通过环境变量覆盖:

RUNIC_HOME=/var/lib/my-agent/runic node agent.js

改用内存存储(测试、临时运行)

import { createRunicClient } from "@runic-labs/sdk";
import { createCache, MemoryCacheStore } from "@runic-labs/cache";
import { createLedger, MemoryLedgerStore } from "@runic-labs/ledger";

const runic = createRunicClient({
  cache: createCache(new MemoryCacheStore()),
  ledger: createLedger(new MemoryLedgerStore()),
});

const cached = await runic.askRunic({ intent: "summarize_pr", params: { repo, pr } });

这正是 benchmarks/reuse-sweepbenchmarks/openrouter-savings 所使用的方式,因此不同脚本运行之间不会保留任何内容。

后续

这个页面有帮助吗?