The OpenAI Agents SDK Node.js is a TypeScript port of the official OpenAI Agents SDK. This library provides the exact same functionality and API as the Python version, but implemented in TypeScript/JavaScript for Node.js environments.
Note: This is an unofficial TypeScript port of the official OpenAI Agents SDK. While it maintains feature parity with the Python version, it's not officially supported by OpenAI.
This TypeScript implementation maintains complete feature parity with the official Python SDK, including:
- ✅ Agent creation and management
- ✅ Tool integration
- ✅ Handoffs between agents
- ✅ Model settings and configuration
- ✅ Response handling
- ✅ Streaming support
The only differences are:
- Language-specific syntax (TypeScript/JavaScript instead of Python)
- Node.js-specific environment handling
- TypeScript type definitions for better development experience
- Agents: LLMs configured with instructions, tools, guardrails, and handoffs
- Handoffs: A specialized tool call used for transferring control between agents
- Tools: Functions that agents can call to perform specific tasks
- Model Settings: Configurable parameters for controlling model behavior
- Install the package:
npm install openai-agents-js
- Set up your environment:
Create a .env
file in your project root:
OPENAI_API_KEY=your_api_key_here
Or set it directly in your shell:
export OPENAI_API_KEY=your_api_key_here
import { Agent, Runner } from 'openai-agents';
const agent = new Agent({
name: "Assistant",
instructions: "You are a helpful assistant"
});
const result = await Runner.run(agent, "Write a haiku about recursion in programming.");
console.log(result.finalOutput);
// Code within the code,
// Functions calling themselves,
// Infinite loop's dance.
import { Agent, Runner } from 'openai-agents';
const spanishAgent = new Agent({
name: "Spanish agent",
instructions: "You only speak Spanish."
});
const englishAgent = new Agent({
name: "English agent",
instructions: "You only speak English"
});
const triageAgent = new Agent({
name: "Triage agent",
instructions: "Handoff to the appropriate agent based on the language of the request.",
handoffs: [spanishAgent, englishAgent]
});
const result = await Runner.run(triageAgent, "Hola, ¿cómo estás?");
console.log(result.finalOutput);
// ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?
import { Agent, Runner, FunctionTool } from 'openai-agents';
const weatherTool = new FunctionTool({
name: 'get_weather',
description: 'Get the weather for a city',
params_json_schema: {
type: 'object',
properties: {
city: { type: 'string' }
},
required: ['city']
},
on_invoke_tool: async ({ input }) => {
return `The weather in ${input.city} is sunny.`;
}
});
const agent = new Agent({
name: "Weather Agent",
instructions: "You are a helpful weather assistant.",
tools: [weatherTool]
});
const result = await Runner.run(agent, "What's the weather in Tokyo?");
console.log(result.finalOutput);
// The weather in Tokyo is sunny.
When you call Runner.run()
, we run a loop until we get a final output:
- We call the LLM, using the model and settings on the agent, and the message history.
- The LLM returns a response, which may include tool calls.
- If the response has a final output, we return it and end the loop.
- If the response has a handoff, we set the agent to the new agent and go back to step 1.
- We process the tool calls (if any) and append the tool responses messages. Then we go to step 1.
You can limit the number of iterations using the maxTurns
parameter.
Final output is determined by:
- If you set an
outputType
on the agent, the final output is when the LLM returns something of that type. - If there's no
outputType
, then the first LLM response without any tool calls or handoffs is considered as the final output.
This is an open-source project developed by the community. Contributions are welcome and appreciated! Here's how you can help:
- Report bugs and issues
- Suggest new features
- Improve documentation
- Submit pull requests
- Share your use cases and examples
To contribute:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
Please ensure your code follows the project's coding standards and includes appropriate tests.
- ✅ Core agent functionality
- ✅ Tool support
- ✅ Handoffs
- ✅ Model settings
- 🔄 Tracing (in development)
- 🔄 Advanced examples (in development)
- 🔄 Logging structure (to be refined)
- Install dependencies:
npm install
- Build the project:
npm run build
- Run tests:
npm test
We'd like to acknowledge the excellent work of the open-source community, especially:
- OpenAI for their API and inspiration
- TypeScript for the language
- Node.js for the runtime
We're committed to continuing to build the Agents SDK as an open source framework so others in the community can expand on our approach.