⌨︎ DIY
Running Swarm.js
Start by creating a Swarm client that interacts with OpenAI’s API.
Copy
Copy
client.run()
The run()
function in Swarm.js is similar to OpenAI's chat.completions.create()
function but with additional features like agent function execution, handoffs, context variables, and multi-turn conversations.
Arguments
ArgumentTypeDescriptionDefault
agent
Agent
The (initial) agent to be called.
(required)
messages
Array
A list of message objects, similar to Chat Completions API messages.
(required)
contextVariables
Object
Additional context variables available to agents.
{}
maxTurns
Number
Maximum number of conversational turns before returning.
Infinity
debug
Boolean
Enables debug logging if true
.
false
Response
Fields
FieldTypeDescription
messages
Array
The list of messages generated during the conversation.
agent
Agent
The last agent to handle the conversation.
contextVariables
Object
The most up-to-date context variables, including any changes during the conversation.
Agents
An Agent
encapsulates instructions and functions (tools). These agents drive the conversation and can decide whether to hand off to another agent.
Copy
Copy
Functions
Functions enable agents to perform actions like processing transactions, looking up information, or transferring control to other agents. Functions are called when the agent needs to perform a specific task.
Copy
Copy
Handoffs and Context Variables
Agents can transfer control to other agents or update contextVariables
based on the conversation flow.
Copy
Copy
Function Schemas
Swarm.js automatically converts functions into JSON schemas, allowing OpenAI’s API to call the appropriate function based on the tool name.
Copy
Copy
Streaming
You can enable streaming in Swarm.js to receive real-time responses from agents.
Copy
Copy
Evaluations
Swarm.js supports various evaluation methods to test and validate your multi-agent systems. You can find example evaluations in the /examples
directory.
Utils
Use the run_demo_loop
utility to test your agents interactively in a REPL environment.
Copy
Copy
Core Contributors
Pulkit Garg (Node.js adaptation)
OpenAI (original Python framework)
This README now emphasizes that Swarm.js is a Node.js implementation of OpenAI Swarm, while maintaining a high standard for documentation and usage clarity.
Last updated