|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +OpenLLMetry-JS is a JavaScript/TypeScript observability framework for LLM applications, built on OpenTelemetry. It provides instrumentation for major LLM providers (OpenAI, Anthropic, etc.) and vector databases, with a unified SDK for easy integration. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Building |
| 12 | + |
| 13 | +```bash |
| 14 | +# Build all packages |
| 15 | +pnpm nx run-many -t build |
| 16 | +# or |
| 17 | +pnpm nx run-many --targets=build |
| 18 | + |
| 19 | +# Build affected packages only |
| 20 | +pnpm nx affected -t build |
| 21 | +``` |
| 22 | + |
| 23 | +### Testing |
| 24 | + |
| 25 | +Each package has its own test command: |
| 26 | + |
| 27 | +```bash |
| 28 | +# Test individual packages |
| 29 | +cd packages/traceloop-sdk |
| 30 | +pnpm test |
| 31 | + |
| 32 | +# Test specific instrumentation |
| 33 | +cd packages/instrumentation-openai |
| 34 | +pnpm test |
| 35 | +``` |
| 36 | + |
| 37 | +You can also use nx to run tests: |
| 38 | + |
| 39 | +```bash |
| 40 | +# Test all packages |
| 41 | +pnpm nx run-many -t test |
| 42 | + |
| 43 | +# Test specific package by name |
| 44 | +pnpm nx test @traceloop/node-server-sdk |
| 45 | + |
| 46 | +# Test only affected packages |
| 47 | +pnpm nx affected -t test |
| 48 | + |
| 49 | +# Test with specific pattern |
| 50 | +pnpm nx run-many -t test --projects="*instrumentation*" |
| 51 | + |
| 52 | +# Run tests in parallel |
| 53 | +pnpm nx run-many -t test --parallel |
| 54 | + |
| 55 | +# Watch mode for development |
| 56 | +pnpm nx test @traceloop/node-server-sdk --watch |
| 57 | +``` |
| 58 | + |
| 59 | +### Linting |
| 60 | + |
| 61 | +```bash |
| 62 | +# Lint individual packages |
| 63 | +cd packages/[package-name] |
| 64 | +pnpm lint |
| 65 | + |
| 66 | +# Fix lint issues |
| 67 | +pnpm lint:fix |
| 68 | +``` |
| 69 | + |
| 70 | +## Architecture |
| 71 | + |
| 72 | +### Monorepo Structure |
| 73 | + |
| 74 | +- **Lerna + Nx**: Manages multiple packages with shared tooling |
| 75 | +- **packages/**: Contains all publishable packages and internal tooling |
| 76 | +- **Rollup**: Used for building packages with TypeScript compilation |
| 77 | + |
| 78 | +### Core Packages |
| 79 | + |
| 80 | +#### `traceloop-sdk` (Main SDK) |
| 81 | + |
| 82 | +- **Path**: `packages/traceloop-sdk/` |
| 83 | +- **Exports**: `@traceloop/node-server-sdk` |
| 84 | +- **Purpose**: Primary entry point that orchestrates all instrumentations |
| 85 | +- **Key Files**: |
| 86 | + - `src/lib/tracing/decorators.ts`: Workflow and task decorators (`@workflow`, `@task`, `@agent`) |
| 87 | + - `src/lib/tracing/tracing.ts`: Core tracing utilities and span management |
| 88 | + - `src/lib/node-server-sdk.ts`: Main initialization logic |
| 89 | + |
| 90 | +#### Instrumentation Packages |
| 91 | + |
| 92 | +Each follows the pattern: `packages/instrumentation-[provider]/` |
| 93 | + |
| 94 | +- **OpenAI**: `@traceloop/instrumentation-openai` |
| 95 | +- **Anthropic**: `@traceloop/instrumentation-anthropic` |
| 96 | +- **Bedrock**: `@traceloop/instrumentation-bedrock` |
| 97 | +- **Vector DBs**: Pinecone, Chroma, Qdrant packages |
| 98 | +- **Frameworks**: LangChain, LlamaIndex packages |
| 99 | + |
| 100 | +#### `ai-semantic-conventions` |
| 101 | + |
| 102 | +- **Path**: `packages/ai-semantic-conventions/` |
| 103 | +- **Purpose**: OpenTelemetry semantic conventions for AI/LLM spans |
| 104 | +- **Key File**: `src/SemanticAttributes.ts` - defines all span attribute constants |
| 105 | + |
| 106 | +### Instrumentation Pattern |
| 107 | + |
| 108 | +All instrumentations extend `InstrumentationBase` from `@opentelemetry/instrumentation`: |
| 109 | + |
| 110 | +1. **Hook Registration**: Wrap target library functions using `InstrumentationModuleDefinition` |
| 111 | +2. **Span Creation**: Create spans with appropriate semantic attributes |
| 112 | +3. **Data Extraction**: Extract request/response data and token usage |
| 113 | +4. **Error Handling**: Capture and record errors appropriately |
| 114 | + |
| 115 | +### Testing Strategy |
| 116 | + |
| 117 | +- **Polly.js**: Records HTTP interactions for consistent test execution |
| 118 | +- **ts-mocha**: TypeScript test runner |
| 119 | +- **Recordings**: Stored in `recordings/` folders for replay testing |
| 120 | + |
| 121 | +## Key Patterns |
| 122 | + |
| 123 | +### Workspace Dependencies |
| 124 | + |
| 125 | +Packages reference each other using `workspace:*` in package.json, managed by pnpm workspaces. |
| 126 | + |
| 127 | +### Decorator Usage |
| 128 | + |
| 129 | +```typescript |
| 130 | +// Workflow spans |
| 131 | +@workflow("my-workflow") |
| 132 | +async function myWorkflow() { } |
| 133 | + |
| 134 | +// Task spans |
| 135 | +@task("my-task") |
| 136 | +async function myTask() { } |
| 137 | +``` |
| 138 | + |
| 139 | +### Manual Instrumentation |
| 140 | + |
| 141 | +```typescript |
| 142 | +import { trace } from "@traceloop/node-server-sdk"; |
| 143 | +const span = trace.withLLMSpan("my-llm-call", () => { |
| 144 | + // LLM operations |
| 145 | +}); |
| 146 | +``` |
| 147 | + |
| 148 | +### Telemetry Configuration |
| 149 | + |
| 150 | +- Anonymous telemetry enabled by default |
| 151 | +- Opt-out via `TRACELOOP_TELEMETRY=FALSE` environment variable |
| 152 | +- Only collected in SDK, not individual instrumentations |
| 153 | + |
| 154 | +## Common Development Tasks |
| 155 | + |
| 156 | +### Adding New LLM Provider |
| 157 | + |
| 158 | +1. Create new instrumentation package in `packages/instrumentation-[provider]/` |
| 159 | +2. Implement instrumentation extending `InstrumentationBase` |
| 160 | +3. Add to main SDK dependencies in `packages/traceloop-sdk/package.json` |
| 161 | +4. Register in SDK initialization |
| 162 | + |
| 163 | +### Running Single Test |
| 164 | + |
| 165 | +```bash |
| 166 | +cd packages/[package-name] |
| 167 | +pnpm test -- --grep "test name pattern" |
| 168 | +``` |
| 169 | + |
| 170 | +### Debugging Instrumentations |
| 171 | + |
| 172 | +Enable OpenTelemetry debug logging: |
| 173 | + |
| 174 | +```bash |
| 175 | +export OTEL_LOG_LEVEL=debug |
| 176 | +``` |
0 commit comments