This project shows how to build a simple AI-driven thread posting agent for Twitter (X). By default it generates 5 threads per day (4 tweets each) across multiple themes, using sample threads to learn tone and style.
It is designed mainly for education and exploration:
- Learn how to connect large language models (LLMs) with simple agents
- Apply basic MLP-style functionality in a practical setting
- See how scheduling, state management, and prompt design work in practice
Based on simple_twitter_ai_agent (single-tweet version).
- Thread generation: Produces 4-tweet connected threads with narrative flow
- AI content generation: Uses Claude Haiku 4.5 for varied, natural, theme-aware content
- Themes and subthemes: Organises posts into themes with subthemes for balance and variety
- Configurable volume: Default is 5 threads/day (~20 tweets), adjustable in code or schedule
- Voice alignment: Learns from sample threads to stay on-brand
- Stateless but progressive: Each run is independent while cycling through themes
- Thread chaining: Posts tweets as proper Twitter threads using reply chains
- Lambda triggers every 5 hours (~5 executions/day)
- Reads current position in the theme/subtheme cycle from DynamoDB
- Generates a 4-tweet thread using Claude Haiku 4.5 with persona and sample context
- Posts tweet 1, then replies to it with tweet 2, replies to that with tweet 3, etc.
- Advances state — after 3 threads per subtheme, moves to the next one
- Full cycle through all subthemes takes ~15 days, then repeats
Each thread follows this pattern:
- Tweet 1: Hook — bold opening statement or provocative question (includes hashtag)
- Tweet 2: Expand — provide depth, a story, or a surprising angle
- Tweet 3: Challenge — shift perspective, push the reader to think differently
- Tweet 4: Close — memorable takeaway or call to reflection
- AWS Lambda: Serverless execution with scheduled triggers
- DynamoDB: Stores cycle state (
ThreadBotState) and sample threads (ThreadBotSampleThreads) - Secrets Manager: Keeps Twitter API and Anthropic API credentials secure
- EventBridge: Automates scheduling (every 5 hours)
const THEMES = {
"YourTheme1": {
description: "Theme description for AI context",
subThemes: ["Subtheme1", "Subtheme2", "Subtheme3", "Subtheme4", "Subtheme5"]
},
// Add more themes...
};Define your bot's identity, voice, beliefs, and what to avoid. The AI uses this to maintain consistent character across all threads.
const TWEETS_PER_THREAD = 4; // Tweets in each thread
const THREADS_PER_SUBTHEME = 3; // Threads per subtheme before advancing
const THREADS_PER_EXECUTION = 1; // Threads per Lambda invocation
const TOTAL_THREADS_PER_DAY = 5; // ~5 executions/day at rate(5 hours)npm install- Edit themes and subthemes in
index.js - Edit persona in
persona.js - Edit sample threads in
setup-dynamodb.js
Store these in AWS Secrets Manager (secret name: thread-bot-secrets):
{
"twitter_api_key": "your_key",
"twitter_api_secret": "your_secret",
"twitter_access_token": "your_token",
"twitter_access_token_secret": "your_token_secret",
"ai_provider_api_key": "your_anthropic_api_key"
}- Go to DynamoDB in AWS Console
- Create table
ThreadBotState:- Partition key:
id(String) - Billing mode: On-demand
- Partition key:
- Create table
ThreadBotSampleThreads:- Partition key:
id(String) - Billing mode: On-demand
- Partition key:
- Go to Secrets Manager in AWS Console
- Click Store a new secret
- Choose Other type of secret
- Add key/value pairs for all 5 credentials (see Secrets Configuration above)
- Name it
thread-bot-secrets
- Go to Lambda in AWS Console
- Click Create function
- Choose Author from scratch
- Function name:
twitter-thread-agent - Runtime: Node.js 22.x
- Architecture: x86_64
- Function name:
- Under General configuration, set timeout to 5 minutes (300 seconds) and memory to 256 MB
- On your local machine, create a deployment package:
zip -r function.zip index.js persona.js package.json node_modules/
- In the Lambda console, upload the
.zipfile under Code source
In the Lambda function configuration, add these environment variables:
| Key | Value |
|---|---|
SECRET_NAME |
thread-bot-secrets |
STATE_TABLE |
ThreadBotState |
SAMPLE_THREADS_TABLE |
ThreadBotSampleThreads |
Add these permissions to the Lambda execution role:
- DynamoDB:
dynamodb:GetItem,dynamodb:PutItem,dynamodb:Scanon both tables - Secrets Manager:
secretsmanager:GetSecretValueon your secret ARN
- In the Lambda function, go to Configuration > Triggers
- Click Add trigger > EventBridge (CloudWatch Events)
- Create a new rule:
- Rule name:
thread-bot-schedule - Schedule expression:
rate(5 hours)
- Rule name:
- Enable the trigger
Update setup-dynamodb.js with your actual sample threads, then run:
node setup-dynamodb.jsUse the Test button in Lambda console with an empty event {} to verify everything works.
- Check CloudWatch Logs at
/aws/lambda/twitter-thread-agent - Logs show cycle progress, current theme/subtheme, and posted thread content
- Recent threads tracking to reduce repetition
- Voice analysis engine to detect tone and sentence patterns automatically
Single-tweet version running on @ProyogiBaba.
For feedback, questions, or contributions, reach out here.
MIT License - Feel free to adapt for your own projects.