Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/openai-agent-sdk-js-telemetry/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Required for OpenAI API calls.
OPENAI_API_KEY=

# Required for PromptLayer tracing.
PROMPTLAYER_API_KEY=

# Optional. Defaults to gpt-5.
OPENAI_MODEL=gpt-5
26 changes: 26 additions & 0 deletions examples/openai-agent-sdk-js-telemetry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dependencies
node_modules/

# Environment
.env
.env.*
!.env.example

# Build output
dist/
build/

# TypeScript
*.tsbuildinfo

# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# OS/editor
.DS_Store
.vscode/
.idea/
57 changes: 57 additions & 0 deletions examples/openai-agent-sdk-js-telemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# PromptLayer OpenAI Agents SDK JS Example

Minimal TypeScript CLI example using PromptLayer's OpenAI Agents SDK tracing integration.

This example follows the PromptLayer OpenAI Agents SDK JavaScript integration pattern and exports traces to `https://api.promptlayer.com/v1/traces`:

```ts
import { Agent, run } from "@openai/agents";
import { instrumentOpenAIAgents } from "promptlayer/openai-agents";

const processor = await instrumentOpenAIAgents();

const agent = new Agent({
name: "PromptLayer Demo Agent",
instructions: "You are concise and practical.",
model: "gpt-5",
});

const result = await run(agent, "Say hello.");
await processor.forceFlush();
await processor.shutdown();
```

## Setup

This docs example intentionally installs the supported PromptLayer JavaScript package from npm through the `promptlayer` dependency in `package.json`. Do not commit a local `file:`, `link:`, or `workspace:` dependency here.

```bash
cd examples/openai-agent-sdk-js-telemetry
npm install
cp .env.example .env
```

Edit `.env`, then run:

```bash
npm run dev
```

You can also pass a custom message:

```bash
npm run dev -- "What is the weather in Tokyo?"
```

## Environment

- `OPENAI_API_KEY` runs the OpenAI Agents SDK.
- `PROMPTLAYER_API_KEY` enables PromptLayer tracing through `instrumentOpenAIAgents()`.
- `OPENAI_MODEL` optionally overrides the default `gpt-5` model.

## Notes

- Requires Node.js 22 or later, matching the OpenAI Agents SDK JavaScript runtime requirement.
- `instrumentOpenAIAgents()` must run before the first agent run.
- `forceFlush()` and `shutdown()` send buffered spans to PromptLayer before the process exits.
- The demo includes a `get_demo_weather` function tool, so tool-call spans are easy to trigger.
73 changes: 73 additions & 0 deletions examples/openai-agent-sdk-js-telemetry/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import "dotenv/config";

import { Agent, run, tool } from "@openai/agents";
import { instrumentOpenAIAgents } from "promptlayer/openai-agents";
import { z } from "zod";

const DEFAULT_MODEL = "gpt-5";
const DEFAULT_MESSAGE = "Use the weather tool for Tokyo, then summarize the forecast in one sentence.";

const getDemoWeather = tool({
name: "get_demo_weather",
description: "Return demo weather for a city.",
parameters: z.object({
city: z.string().describe("City to look up in the demo forecast."),
}),
execute: async ({ city }) => {
const forecasts: Record<string, string> = {
"new york": "New York is 61F and cloudy.",
"san francisco": "San Francisco is 58F and breezy.",
tokyo: "Tokyo is 72F with light rain.",
london: "London is 55F and overcast.",
};

return forecasts[city.trim().toLowerCase()] ?? `${city} is 68F and clear in the demo forecast.`;
},
});

function requiredEnv(name: string): string {
const value = process.env[name]?.trim();
if (!value) {
throw new Error(`Missing ${name}. Add it to .env or export it.`);
}
return value;
}

function parseMessage(): string {
const message = process.argv.slice(2).join(" ").trim();
return message || DEFAULT_MESSAGE;
}

async function main(): Promise<void> {
requiredEnv("OPENAI_API_KEY");
requiredEnv("PROMPTLAYER_API_KEY");

const processor = await instrumentOpenAIAgents();
const model = process.env.OPENAI_MODEL?.trim() || DEFAULT_MODEL;

const agent = new Agent({
name: "PromptLayer Demo Agent",
instructions: [
"You are concise and practical.",
"If the user asks for weather, call get_demo_weather before answering.",
].join(" "),
model,
modelSettings: {
reasoning: { summary: "auto" },
Comment on lines +55 to +56
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid sending reasoning settings for every model override

When users set OPENAI_MODEL to a non-reasoning model such as gpt-4o-mini or another supported Agents SDK model, this example still sends modelSettings.reasoning.summary, which the OpenAI model settings docs describe as model-dependent rather than universally supported. Because the README presents OPENAI_MODEL as a general optional override, those users can hit an API/runtime error before seeing any PromptLayer trace; gate these settings to known reasoning models or document/enforce that only reasoning-capable models are valid here.

Useful? React with 👍 / 👎.

},
tools: [getDemoWeather],
});

try {
const result = await run(agent, parseMessage(), { maxTurns: 4 });
console.log(result.finalOutput);
} finally {
await processor.forceFlush();
await processor.shutdown();
}
}

main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});
25 changes: 25 additions & 0 deletions examples/openai-agent-sdk-js-telemetry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "openai-agent-sdk-js-telemetry",
"version": "0.1.0",
"private": true,
"type": "module",
"engines": {
"node": ">=22"
},
"scripts": {
"dev": "npm run build && node dist/main.js",
"build": "tsc",
"start": "node dist/main.js",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@openai/agents": "latest",
"dotenv": "latest",
"promptlayer": "latest",
"zod": "latest"
},
"devDependencies": {
"@types/node": "latest",
"typescript": "latest"
}
}
13 changes: 13 additions & 0 deletions examples/openai-agent-sdk-js-telemetry/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"types": ["node"],
"outDir": "dist"
},
"include": ["main.ts"]
}
Loading