---
title: '@runic-labs/sdk'
description: askRunic と storeResult — エージェント向け契約のすべて。
sidebar:
  order: 3
---
`@runic-labs/sdk` は、ほとんどの統合で直接インポートする必要がある唯一のパッケージです。`@runic-labs/cache` と `@runic-labs/ledger` を2つの関数でラップします。

```package-install
npm i @runic-labs/sdk
```

## モジュールレベル関数

一般的なケース — 1つのエージェントプロセスとデフォルトのファイルバックドストレージ — では、フリー関数を直接使用します。

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

### `askRunic(decision)`

この完全に一致する決定がすでに解決済みかどうかを確認します。

| Prop | Type | Default | Description |
| - | - | - | - |
| `decision` | `Decision` | - | { intent: string; params: Record<string, unknown> } |

`Promise<CachedEntry | null>` を返します。ミス時は `null`、ヒット時はキャッシュ済みエントリを返し（かつヒットを台帳に記録します）。

| Prop | Type | Default | Description |
| - | - | - | - |
| `signature?` | `string` | - | The normalized signature this entry is keyed on. |
| `artifact?` | `unknown` | - | Whatever was passed to storeResult() originally. |
| `tokensSpent?` | `number` | - | What producing this artifact cost the first time. |
| `createdAt?` | `number` | - | Epoch ms when this entry was first stored. |
| `lastUsedAt?` | `number` | - | Epoch ms of the most recent hit. |
| `hitCount?` | `number` | - | Number of times this entry has been reused. |
| `stale?` | `boolean` | - | Advisory only — Runic never acts on this itself. |

### `storeResult(decision, artifact, tokensSpent)`

決定に対して新しく生成したアーティファクトを記録し、そのコストをログに残します。これにより、将来のヒット時にどれだけ節約できたかがわかります。

| Prop | Type | Default | Description |
| - | - | - | - |
| `decision` | `Decision` | - | Same shape as askRunic's argument. |
| `artifact` | `unknown` | - | Whatever you want returned on a future cache hit. |
| `tokensSpent` | `number` | - | Agent-reported cost of producing this artifact. Runic never measures this itself. |

`Promise<void>` を返します。

## `createRunicClient(options?)`

複数の独立したクライアントが必要な場合 — 最も一般的にはテスト時、またはインメモリストレージを明示的に選択する場合 — は、モジュールレベル関数の代わりにこれを使用します。

```ts
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()),
});

await runic.askRunic({ intent: "summarize_pr", params: { repo, pr } });
await runic.storeResult({ intent: "summarize_pr", params: { repo, pr } }, artifact, tokensUsed);
```

| Prop | Type | Default | Description |
| - | - | - | - |
| `cache?` | `Cache` | - | Defaults to a file-backed cache under .runic/ if omitted. |
| `ledger?` | `Ledger` | - | Defaults to a file-backed ledger under .runic/ if omitted. |

モジュールレベル関数と同じ `askRunic` / `storeResult` 形式を持つ `RunicClient` を返します。これは、渡した `cache`/`ledger` インスタンスにスコープされます。

## 次へ

- [キャッシュ](/cache) — カスタム `CacheStore` 実装を構築する
- [台帳](/ledger) — 台帳サマリーを直接読み取る
- [CLI](/cli) — コマンドラインから状態を確認する
