Build Your First Chainless Agent

Let’s create a simple AI agent using Chainless that can utilize tools and collaborate with other agents to complete a task.

Make sure you have chainless installed. If not, follow the installation guide first.

1

Set Your OpenAI API Key

Chainless supports various LLM providers. In this example, we’ll use OpenAI’s GPT-4 model by setting the required environment variable:

import os

os.environ["OPENAI_API_KEY"] = "sk-***"
2

Define a Tool Function

Tools are callable functions that your agents can use. Let’s define a simple search tool:

from chainless import Tool

def search_web(query: str):
    return f"Searching web for: "{query}""

search_tool = Tool("WebSearch", "Performs a basic web search", search_web)
3

Create Your Agent

Agents are the core of Chainless. They use tools and respond to tasks. Here’s a simple agent setup:

from chainless import Agent
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4")

agent = Agent(
  name="ResearchAgent",
  llm=llm,
  tools=[search_tool],
  system_prompt="Use the WebSearch tool to find information about a topic."
)
4

Run the Agent Manually

You can invoke the agent directly by calling start():

response = agent.start("What's new with AI in 2025?")
print(response)
5

Orchestrate with TaskFlow

For more complex, multi-step tasks, use TaskFlow:

from chainless import TaskFlow

flow = TaskFlow("AI Research Task", verbose=True)
flow.add_agent("Research", agent)
flow.step("Research", input_map={"input": "{{input}}"})

result = flow.run("Tell me the latest news in AI policy.")
print(result["output"]["content"])

Next Steps