Developer Guide

How to Tweet from the
OpenAI Agents SDK

Give your agent a tweet tool two ways: a @function_tool that POSTs to the OpenTweet REST API, or the OpenTweet MCP server attached to the agent. Copy-ready Python below. No X developer account needed.

Last updated: July 2026

7-day free trial. Cancel anytime.

How does an OpenAI Agents SDK agent tweet?

An OpenAI Agents SDK agent tweets by calling a tool you give it. The SDK does not post to X by itself, so you attach a posting capability and the agent invokes it when its plan calls for a tweet. There are two clean ways to do that, and you pick one.

OpenTweet is a Twitter/X scheduler and posting API that holds your X connection, so the agent only ever talks to OpenTweet with one ot_ key.

  • Path A, a function tool: decorate a Python function with @function_tool that POSTs to the OpenTweet REST API.
  • Path B, MCP: attach the OpenTweet MCP server to the agent via the SDK MCP support, for the full set of 43 X tools.

Install the SDK

Both paths use the OpenAI Agents SDK for Python. Install it with pip install openai-agents and set your OPENAI_API_KEY.

Step-by-step: give the agent a tweet tool

1

Get your OpenTweet API key

Create an ot_ key at /developer and connect your X account once. Keep both keys in environment variables.

bash
pip install openai-agents requests

export OPENAI_API_KEY="sk-your_key"
export OPENTWEET_API_KEY="ot_your_key"
2

Path A: define a @function_tool

Decorate a function with @function_tool. The SDK reads its type hints and docstring to build the tool schema, so the agent knows when and how to call it. The body POSTs to OpenTweet.

agent_tweet_tool.py
import os, requests
from agents import Agent, Runner, function_tool

OPENTWEET = "https://opentweet.io/api/v1/posts"
HEADERS = {"Authorization": f"Bearer {os.environ['OPENTWEET_API_KEY']}"}

@function_tool
def post_tweet(text: str, scheduled_date: str | None = None) -> str:
    """Post a tweet to the user's connected X account.

    Args:
        text: The tweet text, up to 280 characters.
        scheduled_date: Optional ISO 8601 time to schedule instead of posting now.
    """
    body = {"text": text}
    if scheduled_date:
        body["scheduled_date"] = scheduled_date
    else:
        body["publish_now"] = True
    r = requests.post(OPENTWEET, headers=HEADERS, json=body, timeout=30)
    r.raise_for_status()
    data = r.json()
    return f"Posted, status={data.get('status')}, id={data.get('id')}"

agent = Agent(
    name="X poster",
    instructions="You post to X when asked. Keep tweets under 280 characters.",
    tools=[post_tweet],
)

result = Runner.run_sync(agent, "Tweet: shipped the OpenAI Agents SDK integration today.")
print(result.final_output)
3

Path B: attach the OpenTweet MCP server

The SDK ships MCP support. Connect the hosted server with MCPServerStreamableHttp and pass it in the agent mcp_servers list. The agent then sees all 43 X tools with no tool code to maintain.

agent_mcp.py
import os, asyncio
from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp

async def main():
    async with MCPServerStreamableHttp(
        name="opentweet",
        params={
            "url": "https://mcp.opentweet.io/mcp",
            "headers": {"Authorization": f"Bearer {os.environ['OPENTWEET_API_KEY']}"},
        },
    ) as opentweet:
        agent = Agent(
            name="X poster",
            instructions="You post and schedule on X using the OpenTweet tools.",
            mcp_servers=[opentweet],
        )
        result = await Runner.run(agent, "Schedule a tweet for tomorrow 9am about our launch.")
        print(result.final_output)

asyncio.run(main())

The one URL you paste is https://mcp.opentweet.io/mcp, a Streamable HTTP endpoint.

What does it cost, and why no X developer account?

OpenTweet is a flat monthly fee with no per-post charge. Building against the X API directly means a developer app and pay-per-use billing on every post.

FactorAgents SDK + OpenTweetAgents SDK + X API directly
PriceFlat $19.99/mo (Pro)X API billing, tiered per project
Per-post fee$0 per post~$0.015 per post, ~$0.20 with a link
X developer accountNot neededRequired, with billing enrollment
Auth in your toolOne ot_ Bearer keyOAuth2 user token, refreshed by you
SchedulingIncluded via scheduled_dateNot supported by the X API
Tools available43 via MCP, or one function toolWhatever you hand-build

Model cost is separate

The $19.99/mo OpenTweet fee covers posting and scheduling. Your OpenAI model usage is billed separately by OpenAI. The tweet tool itself adds no per-post fee.

Draft or schedule for safety

For an autonomous agent, have the tool schedule or save a draft instead of publishing now, then review in the OpenTweet dashboard. Omit publish_now and set scheduled_date to keep a human in the loop.

Frequently asked questions

How do I make an OpenAI Agents SDK agent tweet?

Give the agent a tweet tool. The simplest path is a @function_tool: decorate a Python function whose body POSTs to https://opentweet.io/api/v1/posts with your ot_ Bearer key, then pass it to your Agent in the tools list. When the agent decides to tweet, the SDK calls your function and posts to your connected X account.

Can I use an MCP server with the OpenAI Agents SDK instead of a function tool?

Yes. The OpenAI Agents SDK supports MCP servers. Attach the OpenTweet hosted server with an MCPServerStreamableHttp connection pointing at https://mcp.opentweet.io/mcp, pass it in the mcp_servers list of your Agent, and the agent gets 43 X tools, including post and schedule, with no custom tool code.

Do I need an X developer account to tweet from the Agents SDK?

No. OpenTweet holds the X connection, so the agent only ever calls the OpenTweet API with your ot_ key. You never create an X developer app or enroll in X API billing. Building against the X API directly instead would require a developer app, OAuth, and pay-per-use billing.

Which is better, the function_tool path or the MCP path?

Use @function_tool when you want a single tightly scoped tweet tool with full control over the request. Use the MCP path when you want the full set of 43 X tools, including scheduling, threads, analytics, and media, without writing or maintaining tool code. Both use the same ot_ key.

How much does it cost to tweet from the OpenAI Agents SDK?

OpenTweet is a flat $19.99/mo on the Pro plan, which includes the REST API and MCP access with no per-post fee. That is separate from your OpenAI model usage. Posting through the X API directly instead adds about $0.015 per post, or about $0.20 per post if the tweet contains a link.

Keep exploring

OpenTweet vs the X API

Post, schedule, and automate X without a developer account or the $200/mo minimum.

X DM outreach, human-approved

Find and qualify leads, AI-draft DMs, approve each one, and drip-send from your own account via API or MCP.

Post to X without an API

The clean, account-safe way to post to X from your code or an AI agent.

Twitter MCP Server

Give Claude, Cursor, and OpenClaw the ability to post to X. 44 tools included.

Developer docs

Quickstart, API keys, MCP setup, and the REST reference for posting to X from code or an agent.

Cross-posting reference

One call posts to X, Bluesky and LinkedIn. The platforms field, per-network results, and why a network gets skipped.

Social media API for AI agents

Three ways an agent can post to X, Bluesky and LinkedIn, and the trade-offs of each.

LinkedIn MCP server

Post and schedule to a LinkedIn profile from Claude or Cursor through the official LinkedIn API.

XMCP vs OpenTweet

X's official MCP server bills per API call and cannot schedule. Compare it with the flat-fee hosted MCP.

MCP for AI agents

Connect your AI client to X in under two minutes, no X developer account.

Developer API and keys

REST endpoints, one bearer key, and usage tracking. Build on OpenTweet.

OpenTweet for AI agents

The posting layer for autonomous agents and automations that live on X.

Build an AI Twitter persona

Give your AI agent its own X account. Setup, cadence, and the rules that keep it safe.

Best MCP servers for social media

The 2026 ranked list. How the hosted OpenTweet MCP compares with the official X MCP and others.

Cheapest way to post to X via code

Flat fee vs pay-per-use. Why the $0.20 per-link fee flips the math past ~145 link posts a month.

X API pricing 2026

Every pay-per-use X API endpoint price from docs.x.com in one searchable table.

Bluesky posting API

Post and schedule to Bluesky with one REST call and OAuth, and cross-post to X and LinkedIn in the same request.

Automate LinkedIn posts

Post to LinkedIn from an AI agent, n8n or a script through the official Posts API, with one REST or MCP call.

Ship an agent that tweets

Get your ot_ API key and let your OpenAI Agents SDK agent post to X over a function tool or MCP. No X developer account, no per-post fees.

7-day free trial. Cancel anytime.