-
Notifications
You must be signed in to change notification settings - Fork 7
includes example for openai js sdk #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" }, | ||
| }, | ||
| 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; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When users set
OPENAI_MODELto a non-reasoning model such asgpt-4o-minior another supported Agents SDK model, this example still sendsmodelSettings.reasoning.summary, which the OpenAI model settings docs describe as model-dependent rather than universally supported. Because the README presentsOPENAI_MODELas 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 👍 / 👎.