{"_id":"@calcis/vercel-ai","_rev":"4-7db64d296fc7d3a20ad3f9f548ed3cf8","name":"@calcis/vercel-ai","dist-tags":{"latest":"1.0.4"},"versions":{"1.0.0":{"name":"@calcis/vercel-ai","version":"1.0.0","keywords":["calcis","vercel","ai","sdk","llm","cost","middleware"],"license":"MIT","_id":"@calcis/vercel-ai@1.0.0","maintainers":[{"name":"calcis","email":"calcis.dev@gmail.com"}],"homepage":"https://www.calcis.dev","bugs":{"url":"https://github.com/rc397/Calcis/issues"},"dist":{"shasum":"e78830144380fef3241f56ee3e4ba0aa8213e71b","tarball":"https://registry.npmjs.org/@calcis/vercel-ai/-/vercel-ai-1.0.0.tgz","fileCount":7,"integrity":"sha512-5hYxz121P52MFZJ3Tar+uPsG50oK6K4zB4UHenAsanP48Q59ZIyMUjg1SExg7P5XvIxwBvSDwVxbFi17MmebVw==","signatures":[{"sig":"MEUCICDAw+tfmqC+scOvIO6+Z9upbxhZbOaFwH5KPbcDTcrpAiEA1FZkkrr97FRIdyfdLBNvKUmcvGFTv3+uRVK1XHOd7i0=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":12084},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"ef75cec281a791905f837f7167fde6606d93c92f","scripts":{"build":"tsc","prepublishOnly":"npm run build"},"_npmUser":{"name":"calcis","email":"calcis.dev@gmail.com"},"repository":{"url":"git+https://github.com/rc397/Calcis.git","type":"git"},"_npmVersion":"11.4.2","description":"Calcis cost estimation middleware for Vercel AI SDK","directories":{},"_nodeVersion":"24.4.1","_hasShrinkwrap":false,"devDependencies":{"ai":"^4.0.0","typescript":"^5.0.0","@types/node":"^20.0.0"},"peerDependencies":{"ai":">=3.0.0"},"_npmOperationalInternal":{"tmp":"tmp/vercel-ai_1.0.0_1776596639169_0.7466225306128333","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@calcis/vercel-ai","version":"1.0.1","keywords":["calcis","vercel","ai","sdk","llm","cost","middleware"],"license":"MIT","_id":"@calcis/vercel-ai@1.0.1","maintainers":[{"name":"calcis","email":"calcis.dev@gmail.com"}],"homepage":"https://www.calcis.dev","bugs":{"url":"https://github.com/rc397/Calcis/issues"},"dist":{"shasum":"cc9581eab835eea8c8758478ffe8173a93aa1f7f","tarball":"https://registry.npmjs.org/@calcis/vercel-ai/-/vercel-ai-1.0.1.tgz","fileCount":7,"integrity":"sha512-ONoibubmY9ljDh4qaSsE7Y28AULnxafjKZuJ8FufvSn8zWvMk9BisBo0x/L9YWNy1E1gsIXrZuWJ+rUJhzvLNA==","signatures":[{"sig":"MEUCICEgX+VSJF/oKwq3E16KuRKlvCnUaDJEJzCm+M7L+yKsAiEA7YNnJO4JvQJJgz123fQXetoJVSfTdVXitjHIAOwng9s=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":12695},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"2ac0279a067083da14424b97edaf969cbc69b3c8","scripts":{"build":"tsc","prepublishOnly":"npm run build"},"_npmUser":{"name":"calcis","email":"calcis.dev@gmail.com"},"repository":{"url":"git+https://github.com/rc397/Calcis.git","type":"git"},"_npmVersion":"11.4.2","description":"Calcis cost estimation middleware for Vercel AI SDK","directories":{},"_nodeVersion":"24.4.1","_hasShrinkwrap":false,"devDependencies":{"ai":"^4.0.0","typescript":"^5.0.0","@types/node":"^20.0.0"},"peerDependencies":{"ai":">=3.0.0"},"_npmOperationalInternal":{"tmp":"tmp/vercel-ai_1.0.1_1776940135670_0.19396145217252814","host":"s3://npm-registry-packages-npm-production"}},"1.0.2":{"name":"@calcis/vercel-ai","version":"1.0.2","keywords":["calcis","vercel","ai","sdk","llm","cost","middleware"],"license":"MIT","_id":"@calcis/vercel-ai@1.0.2","maintainers":[{"name":"calcis","email":"calcis.dev@gmail.com"}],"homepage":"https://www.calcis.dev","bugs":{"url":"https://github.com/rc397/Calcis/issues"},"dist":{"shasum":"a561c962ba48d9a390acd35245cc3d6413548f9c","tarball":"https://registry.npmjs.org/@calcis/vercel-ai/-/vercel-ai-1.0.2.tgz","fileCount":7,"integrity":"sha512-w9g5Myy60EZ5eGd0kl6KFjcrx8cNCOn7u+CjeC3rHHu49RWtsfKLxQgyElUxlH1bfwK/WUi8BomwvHXTZcwzwg==","signatures":[{"sig":"MEUCIEQse0KffR/XGPFKi651th01h01lgEQfB5HEW+VlbWSYAiEAz6uf/hzH7Pkyryimt9PXQIKuxwtaBfFuJjn0nUFBT54=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":13025},"main":"dist/index.js","types":"dist/index.d.ts","gitHead":"2ac0279a067083da14424b97edaf969cbc69b3c8","scripts":{"build":"tsc","prepublishOnly":"npm run build"},"_npmUser":{"name":"calcis","email":"calcis.dev@gmail.com"},"repository":{"url":"git+https://github.com/rc397/Calcis.git","type":"git"},"_npmVersion":"11.4.2","description":"Calcis cost estimation middleware for Vercel AI SDK","directories":{},"_nodeVersion":"24.4.1","_hasShrinkwrap":false,"devDependencies":{"ai":"^4.0.0","typescript":"^5.0.0","@types/node":"^20.0.0"},"peerDependencies":{"ai":">=3.0.0"},"_npmOperationalInternal":{"tmp":"tmp/vercel-ai_1.0.2_1776941431944_0.7381407270479461","host":"s3://npm-registry-packages-npm-production"}},"1.0.4":{"name":"@calcis/vercel-ai","version":"1.0.4","description":"Calcis cost estimation middleware for Vercel AI SDK","license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsc","prepublishOnly":"npm run build"},"keywords":["calcis","vercel","ai","sdk","llm","cost","middleware"],"peerDependencies":{"ai":">=3.0.0"},"devDependencies":{"ai":"^4.0.0","typescript":"^5.0.0","@types/node":"^20.0.0"},"repository":{"type":"git","url":"git+https://github.com/rc397/Calcis.git"},"homepage":"https://www.calcis.dev","_id":"@calcis/vercel-ai@1.0.4","gitHead":"862b57a8fc4e016ca36906c9c0ddea082f4c38b9","bugs":{"url":"https://github.com/rc397/Calcis/issues"},"_nodeVersion":"24.4.1","_npmVersion":"11.4.2","dist":{"integrity":"sha512-7Famz1l1QFqWsQ5SXuJ08sQXAfiMYN64iWuZMrkbOcjF7sV5tlPBfdjr6cG/6PuzHDbAkVQUHie7QSJWt5esdA==","shasum":"2c396a20bb04fc11613a8277d49f33416d7f4357","tarball":"https://registry.npmjs.org/@calcis/vercel-ai/-/vercel-ai-1.0.4.tgz","fileCount":7,"unpackedSize":12988,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCID6m4WyieMbSXdvxWHd/O82uVK0mhnFbBBPfVnN7xy6KAiBhvF3ISLG5H3KV+fTYIXuLzDltFR45ZPDIFfkd6V3sFQ=="}]},"_npmUser":{"name":"calcis","email":"calcis.dev@gmail.com"},"directories":{},"maintainers":[{"name":"calcis","email":"calcis.dev@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/vercel-ai_1.0.4_1777117470692_0.7635210050468006"},"_hasShrinkwrap":false}},"time":{"created":"2026-04-19T11:03:59.086Z","modified":"2026-04-25T11:44:30.949Z","1.0.0":"2026-04-19T11:03:59.302Z","1.0.1":"2026-04-23T10:28:55.805Z","1.0.2":"2026-04-23T10:50:32.093Z","1.0.4":"2026-04-25T11:44:30.848Z"},"bugs":{"url":"https://github.com/rc397/Calcis/issues"},"license":"MIT","homepage":"https://www.calcis.dev","keywords":["calcis","vercel","ai","sdk","llm","cost","middleware"],"repository":{"type":"git","url":"git+https://github.com/rc397/Calcis.git"},"description":"Calcis cost estimation middleware for Vercel AI SDK","maintainers":[{"name":"calcis","email":"calcis.dev@gmail.com"}],"readme":"# @calcis/vercel-ai\r\n\r\nCalcis cost-estimation middleware for the [Vercel AI SDK](https://sdk.vercel.ai).\r\n\r\n> **Live pricing for 25+ models, side-by-side comparisons, and a web estimator: https://calcis.dev**\r\n>\r\n> - Full price index: https://calcis.dev/models\r\n> - Compare models: https://calcis.dev/compare\r\n> - API reference: https://calcis.dev/api-docs\r\n\r\nWrap any `LanguageModelV1` with `calcisMiddleware` and get a one-line\r\ncost estimate for every `generateText` / `streamText` call, plus a\r\nrolling session total.\r\n\r\n## Install\r\n\r\n```bash\r\nnpm install @calcis/vercel-ai ai\r\n```\r\n\r\nYou also need a Calcis API key (Pro tier or above). Get one at\r\n[calcis.dev/dashboard](https://calcis.dev/dashboard). Browse every\r\nsupported model at [calcis.dev/models](https://calcis.dev/models).\r\n\r\n## Usage\r\n\r\n```ts\r\nimport { generateText, wrapLanguageModel } from \"ai\";\r\nimport { anthropic } from \"@ai-sdk/anthropic\";\r\nimport { calcisMiddleware } from \"@calcis/vercel-ai\";\r\n\r\nconst calcis = calcisMiddleware({\r\n  apiKey: process.env.CALCIS_API_KEY!,\r\n});\r\n\r\nconst model = wrapLanguageModel({\r\n  model: anthropic(\"claude-sonnet-4-6\"),\r\n  middleware: calcis,\r\n});\r\n\r\nconst { text } = await generateText({\r\n  model,\r\n  prompt: \"Explain quantum computing in three bullets\",\r\n});\r\n\r\nconsole.log(`Session total: $${calcis.sessionTotal().toFixed(4)}`);\r\n```\r\n\r\nEvery call now emits:\r\n\r\n```\r\n[calcis] claude-sonnet-4-6 · 243 in · ~650 out · $0.0105 · session: $0.0105 (1 calls)\r\n```\r\n\r\n### Streaming\r\n\r\n`streamText` works the same: the estimate fires once at stream\r\nstart.\r\n\r\n```ts\r\nconst { textStream } = await streamText({\r\n  model,\r\n  prompt: \"Write a limerick about quantum computing\",\r\n});\r\nfor await (const chunk of textStream) process.stdout.write(chunk);\r\n```\r\n\r\n## Config\r\n\r\n```ts\r\ncalcisMiddleware({\r\n  apiKey: string,                    // required: calc_… key\r\n  verbose?: boolean,                 // default true: logs per-call summary\r\n  onEstimate?: (e) => void,          // optional structured sink\r\n});\r\n```\r\n\r\nThe returned middleware is a `LanguageModelV1Middleware` (so it\r\ncomposes with any other `wrapLanguageModel` middleware) *and* exposes\r\nthree helpers:\r\n\r\n```ts\r\ncalcis.sessionTotal(); // cumulative cost across calls\r\ncalcis.callCount();    // number of LLM calls so far\r\ncalcis.resetSession(); // zero the counters (e.g. per request)\r\n```\r\n\r\n## How it works\r\n\r\nThe middleware attaches to both `wrapGenerate` and `wrapStream`. On\r\neach call:\r\n\r\n1. Flatten the SDK prompt (role-tagged messages, multi-modal parts)\r\n   into a single text string.\r\n2. Read the model ID from `model.modelId`.\r\n3. Fire a POST to `https://www.calcis.dev/api/v1/estimate` in the\r\n   background: **not** awaited: so the real LLM call runs in\r\n   parallel. Calcis latency never blocks generation.\r\n4. When the estimate resolves, log it and invoke `onEstimate`.\r\n\r\nNon-text prompt parts (images, files, tool results) are skipped -\r\ntheir cost contribution is provider-specific and Calcis estimates\r\nfrom the text portion today.\r\n\r\n## Failure mode\r\n\r\nThe middleware never throws. If the Calcis API is unreachable,\r\nreturns an error, or the response is malformed, the handler silently\r\nskips the estimate for that call: the underlying `generateText`\r\nkeeps running. Cost estimation is a nice-to-have; your product is\r\nnot.\r\n\r\n## Compatibility\r\n\r\nTested against **ai 4.3**. The `LanguageModelV1Middleware` interface\r\nhas been stable across the 3.x → 4.x series. If a future v2 interface\r\nlands, this package will grow a v2 code path rather than be rewritten.\r\n\r\n## Links\r\n\r\n- [Web App](https://www.calcis.dev)\r\n- [API Docs](https://www.calcis.dev/api-docs)\r\n- [CLI (`calcis`)](https://www.npmjs.com/package/calcis)\r\n- [LangChain integration (`@calcis/langchain`)](https://www.npmjs.com/package/@calcis/langchain)\r\n- [LlamaIndex integration (`@calcis/llamaindex`)](https://www.npmjs.com/package/@calcis/llamaindex)\r\n- [MCP server (`@calcis/mcp-server`)](https://www.npmjs.com/package/@calcis/mcp-server)\r\n\r\n## License\r\n\r\nMIT\r\n","readmeFilename":"README.md"}