Cloudflare Workers AI Integration

Opik provides seamless integration with Cloudflare Workers AI allowing you to trace, monitor, and debug your Cloudflare Workers AI applications.

Features

  • Comprehensive Tracing: Automatically trace Cloudflare Workers AI requests
  • Hierarchical Visualization: View your Cloudflare Workers AI requests as structured traces with parent-child relationships
  • Custom Tagging: Add custom tags to organize and filter your traces

Installation

Option 1: Using npm

$npm install opik

Option 2: Using yarn

$yarn add opik

Requirements

  • Node.js ≥ 18
  • Opik SDK

Enable Node.js compatibility mode

The Opik SDK requires Node.js compatibility mode to be enabled in Cloudflare Workers AI. You can enable it by adding the following to your wrangler.toml file:

1{
2 "compatibility_flags": [
3 "nodejs_compat"
4 ],
5 "compatibility_date": "2024-09-23"
6}

See the Cloudflare documentation for more details.

Basic Usage

Using with LLM Calls

Here is an example of how to use the Opik SDK with Cloudflare Workers AI:

1/**
2 * Welcome to Cloudflare Workers! This is your first worker.
3 *
4 * - Run `npm run dev` in your terminal to start a development server
5 * - Open a browser tab at http://localhost:8787/ to see your worker in action
6 * - Run `npm run deploy` to publish your worker
7 *
8 * Bind resources to your worker in `wrangler.jsonc`. After adding bindings, a type definition for the
9 * `Env` object can be regenerated with `npm run cf-typegen`.
10 *
11 * Learn more at https://developers.cloudflare.com/workers/
12 */
13import { Opik } from 'opik';
14
15const client = new Opik({
16 apiKey: '<API-KEY>',
17 apiUrl: 'https://www.comet.com/opik/api',
18 projectName: 'demo-cloudflare-workers-ai',
19 workspaceName: '<YOUR-WORKSPACE>',
20});
21
22export default {
23 async fetch(request, env) {
24 const input = 'What is the origin of the phrase Hello, World';
25 const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
26 prompt: input,
27 });
28
29 const someTrace = client.trace({
30 name: `Trace`,
31 input: {
32 prompt: input,
33 },
34 output: {
35 response: response,
36 },
37 });
38
39 const someSpan = someTrace.span({
40 name: `Span`,
41 type: 'llm',
42 input: {
43 prompt: input,
44 },
45 output: {
46 response: response,
47 },
48 });
49 someSpan.end();
50
51 someTrace.end();
52
53 await client.flush();
54
55 return new Response(JSON.stringify(response));
56 },
57} satisfies ExportedHandler<Env>;