Skip to content
LangChain

Content Generation Agent with LangChain

Build an AI content generation agent that researches topics, writes blog posts and social media content with consistent brand voice — using LangChain.

contentblogsocial mediawriting

Working Code

LangChain
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
@tool
def web_search(query: str) -> str:
"""Research a topic before writing content."""
from tavily import TavilyClient
client = TavilyClient()
results = client.search(query, max_results=3)
return "\n\n".join(r["content"] for r in results["results"])
@tool
def save_content(filename: str, content: str) -> str:
"""Save generated content to a file."""
Path(f"output/{filename}").write_text(content)
return f"Saved to output/{filename}"
model = ChatOpenAI(model="gpt-4o")
model_with_tools = model.bind_tools([web_search, save_content])
response = model_with_tools.invoke([
("system", "You are a content writer. Research the topic first, then write engaging content. Save the final output using save_content."),
("user", "Write a blog post about the benefits of AI agents in customer service"),
])

Step by Step

1

Install dependencies

Install LangChain and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the LangChain agent with your tools, set the system prompt, and execute a query.

Ready to build with LangChain?

Generate a production-ready project with LangChain pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.