> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-mintlify-8476678c.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Agents SDK

> Use W&B Weave with the OpenAI Agents SDK to track and monitor your agentic applications

You can use W\&B Weave with the OpenAI Agents SDK to trace and monitor your agentic applications. This guide shows you how to install the integration, initialize Weave in your agent code, and view the resulting traces in the Weave dashboard so you can debug and evaluate multi-agent workflows.

<Tabs>
  <Tab title="Python">
    The [OpenAI Agents Python SDK](https://github.com/openai/openai-agents-python) is a lightweight framework for building multi-agent workflows.

    ## Installation

    Install the required dependencies using `pip`:

    ```bash theme={null}
    pip install weave openai-agents
    ```

    ## Get started

    To use the OpenAI Agents SDK with Weave:

    1. Initialize Weave with your project name.
    2. Add the Weave tracing processor to your agents.
    3. Create and run your agents as usual.

    The following code sample creates an OpenAI Agent and integrates it with Weave for traceability. The sample initializes a Weave project and sets up the `WeaveTracingProcessor` to capture execution traces. A `Weather` data model represents weather information. The `get_weather` function, decorated as a tool the agent can use, returns a sample weather report. An agent named `Hello world` uses basic instructions and access to the weather tool. The `main` function runs the agent asynchronously with a sample input (`What's the weather in Tokyo?`) and prints the final response.

    ```python lines theme={null}
    from pydantic import BaseModel
    from agents import Agent, Runner, function_tool
    import agents
    import weave
    import asyncio

    weave.init("openai-agents")

    class Weather(BaseModel):
        city: str
        temperature_range: str
        conditions: str

    @function_tool
    def get_weather(city: str) -> Weather:
        return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.")

    agent = Agent(
        name="Hello world",
        instructions="You are a helpful agent.",
        tools=[get_weather]
    )

    async def main():
        result = await Runner.run(agent, input="What's the weather in Tokyo?")    
        print(result.final_output)

    if __name__ == "__main__":
        asyncio.run(main())
    ```

    ## View traces

    When you run the previous code sample, Weave generates a link to the Weave dashboard. Open the link to inspect traces from your agent run.
  </Tab>

  <Tab title="TypeScript">
    The [OpenAI Agents Node SDK](https://github.com/openai/openai-node) is a lightweight framework for building multi-agent workflows.

    ## Installation

    Install the required dependencies using `npm`. Weave's TypeScript integration requires `@openai/agents` version `0.4.15` or later:

    ```bash theme={null}
    npm install weave @openai/agents zod
    ```

    ## Get started

    In most Node.js environments, you don't need manual instrumentation. Weave automatically instruments `@openai/agents` when the module loads.

    Automatic instrumentation works by registering a module loader hook:

    * **CommonJS projects:** No additional configuration is required.
    * **ESM projects:** Start Node with the `--import=weave/instrument` flag so the instrumentation loads before other modules.

    If you need help setting up your project or determining which type you're using, see [TypeScript SDK: Third-Party Integration Guide](/weave/guides/integrations/js).

    The following code sample creates an OpenAI Agent and integrates it with Weave for traceability. Replace `your-team-name/your-project-name` in the Weave project initialization with your entity and project. The sample defines a `get_weather` tool that returns a sample weather report, then configures an agent named `Hello world` with basic instructions and access to the weather tool. The `main` function runs the agent with a sample input (`What's the weather in Tokyo?`) and logs the final response.

    ```typescript twoslash lines theme={null}
    // @noErrors
    import * as weave from "weave";
    import { Agent, run, tool } from "@openai/agents";
    import { z } from "zod";

    const getWeather = tool({
      name: "get_weather",
      description: "Get the current weather for a given city.",
      parameters: z.object({
        city: z.string().describe("The name of the city"),
      }),
      async execute({ city }) {
        return `${city}: 14-20C, Sunny with wind.`;
      },
    });

    async function main() {
      // UPDATE to your project info.
      await weave.init("your-team-name/your-project-name");

      const agent = new Agent({
        name: "Hello world",
        instructions: "You are a helpful agent.",
        tools: [getWeather],
      });

      const result = await run(agent, [
        { role: "user", content: "What's the weather in Tokyo?" },
      ]);
      console.log(result.finalOutput);
    }

    main();
    ```

    In CommonJS, the `import * as weave from "weave"` statement must appear before `import { Agent, run, tool } from "@openai/agents"` for automatic instrumentation. If your code cannot follow this ordering, use the manual instrumentation technique that follows.

    ### Manual instrumentation

    You only need manual instrumentation in cases where the module loader hook can't run, such as:

    * Bundlers that bundle dependencies into a single file.
    * Environments where Node CLI flags can't be passed.
    * Dynamic module loading patterns that bypass the loader hook.

    In these cases, you can explicitly register the instrumentation with `instrumentOpenAIAgents()`:

    ```typescript twoslash lines theme={null}
    // @noErrors
    import * as weave from "weave";

    await weave.init("openai-agents");
    await weave.instrumentOpenAIAgents();
    ```

    For full control over the tracing processor, such as for custom processor configuration or conditional registration, create and register one manually:

    ```typescript twoslash lines theme={null}
    // @noErrors
    import { addTraceProcessor } from "@openai/agents";
    import { createOpenAIAgentsTracingProcessor } from "weave";

    ...

    const processor = createOpenAIAgentsTracingProcessor();
    addTraceProcessor(processor);
    ```
  </Tab>
</Tabs>
