Autonomous Space Agent

July 30, 2026 · View on GitHub

Let AI agents join, listen, and speak in X/Twitter Spaces — powered by voice AI.

XActions integrates the xspace-agent SDK to enable AI agents to autonomously participate in live X Spaces. Agents can join a Space, transcribe other speakers in real time, generate intelligent responses with an LLM, and speak them back into the Space via text-to-speech.

How It Works

┌──────────────────┐
│   X Space (live)  │
└────────┬─────────┘
         │  Puppeteer + headless browser
┌────────▼─────────┐
│ Join Space &      │  Authenticates → joins → requests speaker access
│ Capture Audio     │  Hooks WebRTC to capture PCM audio
└────────┬─────────┘
         │
   ┌─────┼─────┐
   │     │     │
   ▼     ▼     ▼
  STT   LLM   TTS
Whisper  Any   ElevenLabs
(Groq/  provider OpenAI
OpenAI)  ↓     Browser
         │
         ▼
  Agent speaks in Space
  1. A headless Chromium browser logs into X with your session cookies
  2. The browser joins the Space and requests speaker access
  3. Audio from other speakers is captured via WebRTC and transcribed (Whisper STT)
  4. Transcriptions are sent to your chosen LLM (OpenAI, Claude, or Groq) with a system prompt
  5. The LLM response is converted to speech (ElevenLabs, OpenAI TTS, or browser TTS)
  6. The synthesized audio is injected back into the Space via WebRTC

Prerequisites

  • Node.js 18+
  • X (Twitter) account with access to Spaces
  • AI provider API key — at least one of: OpenAI, Anthropic (Claude), or Groq
  • Optional: ElevenLabs API key for high-quality TTS voices

Installation

npm install xactions xspace-agent

xspace-agent is an optional peer dependency — it's only needed if you want to use the Space agent features.

Authentication

The agent needs your X session cookies. Get them from your browser:

  1. Open x.com and log in
  2. Open DevTools (F12 or Cmd+Option+I)
  3. Go to Application > Cookies > https://x.com
  4. Copy auth_token and ct0
export X_AUTH_TOKEN="your_auth_token_value"
export X_CT0="your_ct0_value"

Quick Start

Via MCP Server (Claude Desktop, Cursor, etc.)

If you already have XActions configured as an MCP server, the Space agent tools are available immediately:

"Join this Space as an AI agent: https://x.com/i/spaces/1abc..."

Claude will call the x_space_join tool and your agent will enter the Space.

Via Node.js

import { joinSpace, leaveSpace, getSpaceAgentStatus, getSpaceTranscript } from 'xactions/spaces/agent';

// Join a Space
const result = await joinSpace({
  url: 'https://x.com/i/spaces/1abc123',
  provider: 'openai',                    // or 'claude', 'groq'
  apiKey: process.env.OPENAI_API_KEY,
  systemPrompt: 'You are a helpful AI participant. Keep responses under 2 sentences.',
});
console.log(result);
// { success: true, message: '✅ Joined Space: ...', status: 'listening' }

// Check status
const status = getSpaceAgentStatus();
console.log(status);
// { active: true, duration: '120s', transcriptions: 15, responses: 3 }

// Get transcriptions
const transcript = getSpaceTranscript({ limit: 10 });
console.log(transcript.transcriptions);

// Leave the Space
const summary = await leaveSpace();
console.log(summary);
// { success: true, duration: '300s', transcriptions: 42, responses: 8 }

Via CLI

# Join a Space with default settings (uses env vars)
xactions space join https://x.com/i/spaces/1abc123

# Join with a custom system prompt
xactions space join https://x.com/i/spaces/1abc123 \
  --provider claude \
  --system-prompt "You are a crypto analyst. Share concise market insights."

MCP Tools Reference

ToolDescriptionInput
x_space_joinJoin a Space with an autonomous AI voice agent{ url, provider?, apiKey?, systemPrompt?, model?, voiceId?, headless? }
x_space_leaveLeave the active Space and get session summary{}
x_space_statusGet agent status (duration, transcription/response counts){}
x_space_transcriptGet recent transcriptions from the active Space{ limit? }
x_get_spacesDiscover live or scheduled Spaces{ filter: 'live'|'scheduled'|'all', topic?, limit? }
x_scrape_spaceScrape metadata from a specific Space{ url }

Configuration

joinSpace() Parameters

ParameterTypeDefaultDescription
urlstringrequiredSpace URL (e.g. https://x.com/i/spaces/1abc123)
providerstring'openai'LLM provider: openai, claude, groq
apiKeystringfrom envAI provider API key
systemPromptstringgenericCustom personality/behavior prompt
modelstringprovider defaultLLM model name (e.g. gpt-4o, claude-sonnet-4-20250514)
voiceIdstringprovider defaultTTS voice ID (for ElevenLabs or OpenAI)
ttsProviderstring'openai'TTS provider: openai, elevenlabs, browser
sttProviderstring'groq'STT provider: groq, openai
headlessbooleantrueRun browser in headless mode
maxHistorynumber—Max conversation history messages for context
silenceThresholdnumber1500Milliseconds of silence before finalizing audio chunk
turnDelaynumber—Delay (ms) before agent responds after silence
chromePathstringauto-detectPath to Chrome/Chromium executable

Environment Variables

VariablePurpose
X_AUTH_TOKENX session cookie (auth_token)
X_CT0X CSRF token (ct0)
OPENAI_API_KEYOpenAI API key (LLM + STT + TTS)
ANTHROPIC_API_KEYAnthropic API key (Claude LLM)
GROQ_API_KEYGroq API key (LLM + fast STT)
ELEVENLABS_API_KEYElevenLabs API key (premium TTS)
DEEPGRAM_API_KEYDeepgram API key (STT)
XSPACE_AI_PROVIDERDefault LLM provider (openai, claude, groq)
XSPACE_SYSTEM_PROMPTDefault system prompt for the agent
XSPACE_TTS_PROVIDERDefault TTS provider
XSPACE_STT_PROVIDERDefault STT provider
XSPACE_VOICE_IDDefault TTS voice ID

Events

The agent emits events you can listen to for logging, monitoring, or custom behavior:

EventDataDescription
transcription{ speaker, text }Another speaker's audio was transcribed
response{ text }The agent generated and spoke a response
statusstringAgent lifecycle change (launching, authenticating, joining, listening, speaking)
errorErrorAn error occurred
space-ended—The Space was closed by the host

What the Agent Can Do

  • Join any live X Space (public or invite-only if your account has access)
  • Request speaker access automatically (host must approve)
  • Listen to and transcribe all speakers in real time
  • Generate contextually relevant responses using your chosen LLM
  • Speak responses aloud in the Space via TTS
  • Track conversation context, sentiment, and topics
  • Handle turn-taking and avoid interrupting speakers
  • Leave gracefully when the Space ends or on command
  • Report session metrics (transcription count, response count, duration)

Limitations

  • One Space at a time — the agent is a singleton; call leaveSpace() before joining another
  • Speaker access requires host approval — the agent requests to speak but cannot force it
  • Cannot host Spaces — hosting requires 600+ followers (X restriction)
  • Cannot record Spaces — recording is controlled by the host
  • Audio quality depends on providers — ElevenLabs gives the best TTS quality, Groq gives the fastest STT

Example: Crypto Alpha Space Agent

import { joinSpace } from 'xactions/spaces/agent';

await joinSpace({
  url: 'https://x.com/i/spaces/1abc123',
  provider: 'claude',
  apiKey: process.env.ANTHROPIC_API_KEY,
  systemPrompt: `You are a knowledgeable crypto market analyst participating in an X Space.
    - Share concise, data-driven insights
    - Reference on-chain metrics when relevant
    - Keep responses under 3 sentences
    - Be respectful of other speakers' opinions
    - If asked about price predictions, caveat with "not financial advice"`,
  model: 'claude-sonnet-4-20250514',
  ttsProvider: 'elevenlabs',
  voiceId: 'pNInz6obpgDQGcFmaJgB',  // ElevenLabs "Adam" voice
});

Example: Multi-Agent Debate

Using the xspace-agent SDK directly, you can coordinate multiple AI agents in a single Space:

import { AgentTeam } from 'xspace-agent';

const team = new AgentTeam({
  auth: { token: process.env.X_AUTH_TOKEN, ct0: process.env.X_CT0 },
  agents: [
    {
      name: 'Optimist',
      ai: {
        provider: 'openai',
        apiKey: process.env.OPENAI_API_KEY,
        systemPrompt: 'You always see the positive side of technology trends.',
      },
    },
    {
      name: 'Skeptic',
      ai: {
        provider: 'claude',
        apiKey: process.env.ANTHROPIC_API_KEY,
        systemPrompt: 'You question hype and push for evidence-based reasoning.',
      },
    },
  ],
});

await team.join('https://x.com/i/spaces/1abc123');