X (Twitter)

XTools allows an Agent to interact with X, providing functionality for posting, messaging, and searching tweets.

Prerequisites

Install the required library:

1uv pip install tweepy
InfoTweepy is a Python library for interacting with the X API.

Setup

  1. Create X Developer Account

    • Visit developer.x.com and apply for developer access
    • Create a new project and app in your developer portal
  2. Generate API Credentials

    • Navigate to your app's "Keys and tokens" section
    • Generate and copy these credentials:
      • API Key & Secret
      • Bearer Token
      • Access Token & Secret
  3. Configure Environment

    1export X_CONSUMER_KEY=your_api_key
    2export X_CONSUMER_SECRET=your_api_secret
    3export X_ACCESS_TOKEN=your_access_token
    4export X_ACCESS_TOKEN_SECRET=your_access_token_secret
    5export X_BEARER_TOKEN=your_bearer_token

Example

1from kern.agent import Agent
2from kern.tools.x import XTools
3
4# Initialize the X toolkit
5x_tools = XTools(
6 wait_on_rate_limit=True # Retry when rate limits are reached
7)
8
9# Create an agent equipped with X toolkit
10agent = Agent(
11 instructions=[
12 "Use X tools to interact as the authorized user",
13 "Generate appropriate content when asked to create posts",
14 "Only post content when explicitly instructed",
15 "Respect X's usage policies and rate limits",
16 ],
17 tools=[x_tools],
18 )
19
20# Search for posts
21agent.print_response("Search for recent posts about AI agents", markdown=True)
22
23# Create and post a tweet
24agent.print_response("Create a post about AI ethics", markdown=True)
25
26# Get user timeline
27agent.print_response("Get my timeline", markdown=True)
28
29# Reply to a post
30agent.print_response(
31 "Can you reply to this [post ID] post as a general message as to how great this project is: https://x.com/AgnoAgi",
32 markdown=True,
33)
34
35# Get information about a user
36agent.print_response("Can you retrieve information about this user https://x.com/AgnoAgi ", markdown=True)
37
38# Send a direct message
39agent.print_response(
40 "Send direct message to the user @AgnoAgi telling them I want to learn more about them and a link to their community.",
41 markdown=True,
42)
43
44# Get user profile
45agent.print_response("Get my X profile", markdown=True)
Note

Check out the Tweet Analysis Agent for a more advanced example.

Toolkit Params

ParameterTypeDefaultDescription
bearer_tokenstrNoneBearer token for authentication
consumer_keystrNoneConsumer key for authentication
consumer_secretstrNoneConsumer secret for authentication
access_tokenstrNoneAccess token for authentication
access_token_secretstrNoneAccess token secret for authentication
include_post_metricsboolFalseInclude post metrics (likes, retweets, etc.) in search results
wait_on_rate_limitboolFalseRetry when rate limits are reached

Toolkit Functions

FunctionDescription
create_postCreates and posts a new post
reply_to_postReplies to an existing post
send_dmSends a direct message to a X user
get_user_infoRetrieves information about a X user
get_home_timelineGets the authenticated user's home timeline
search_postsSearches for tweets

You can use include_tools or exclude_tools to modify the list of tools the agent has access to. Learn more about selecting tools.

Developer Resources