Manual instrumentation SDK for custom TypeScript agents
Observatory’s custom instrumentation SDK provides full control over what and how you instrument your AI agents. Use this for custom agents, in-house frameworks, or any stack not covered by automatic integrations.
import { run } from '@contextcompany/custom';// Create a runconst r = run({ sessionId: 'session-abc', conversational: true,});// Set the user promptr.prompt('What is the weather in San Francisco?');// Record an LLM stepconst step = r.step();step.prompt(JSON.stringify(messages));step.response('It is currently 72°F and sunny in San Francisco.');step.model('gpt-4o');step.tokens({ uncached: 120, cached: 30, completion: 45 });step.end();// Set the final response and sendr.response('It is 72°F in San Francisco.');await r.end();
import { run } from '@contextcompany/custom';const r = run({ sessionId: 'session-123' });r.prompt('What is the weather in Tokyo?');// LLM decides to call a toolconst step1 = r.step();step1.prompt(JSON.stringify(messages));step1.response('I need to call get_weather');step1.model('gpt-4');step1.end();// Record the tool callconst tc = r.toolCall('get_weather');tc.args({ city: 'Tokyo', unit: 'celsius' });tc.result({ temperature: 22, conditions: 'cloudy' });tc.end();// LLM processes tool resultconst step2 = r.step();step2.prompt(JSON.stringify(messagesWithToolResult));step2.response('It is 22°C and cloudy in Tokyo.');step2.model('gpt-4');step2.end();// Finalizer.response('It is 22°C and cloudy in Tokyo.');await r.end();
import { run } from '@contextcompany/custom';const r = run();r.prompt('Translate this to French');try { const step = r.step(); step.prompt('Translate...'); const result = await callLLM(); step.response(result); step.end(); r.response(result); await r.end();} catch (error) { // Auto-ends any un-ended children with error status await r.error(String(error));}
import { sendRun } from '@contextcompany/custom';await sendRun({ prompt: { user_prompt: 'What is the capital of France?', system_prompt: 'You are a helpful assistant.', }, response: 'The capital of France is Paris.', startTime: new Date('2025-01-01T00:00:00Z'), endTime: new Date('2025-01-01T00:00:02Z'), metadata: { userId: 'user_123', source: 'web-app', }, steps: [ { prompt: JSON.stringify([{ role: 'user', content: 'What is the capital of France?' }]), response: 'The capital of France is Paris.', model: 'gpt-4o', tokens: { uncached: 100, completion: 20 }, startTime: new Date('2025-01-01T00:00:00Z'), endTime: new Date('2025-01-01T00:00:01Z'), }, ],});
// Good: structured, searchabler.metadata({ userId: 'user_123', environment: 'production', modelFamily: 'gpt-4', feature: 'chat',});// Avoid: unstructured, hard to queryr.metadata({ info: 'user_123 in production using gpt-4 for chat',});