Sequential Workflows
A single agent can do a lot, but some problems are better solved by a chain of specialists. Sequential workflows pass the output from one agent as the input to the next — like an assembly line where each station adds value.
Why Chain Agents?
Each agent has focused instructions, a suitable model, and the right temperature for its job:
ResearchAgent (low temp, factual) ↓ structured factsWriterAgent (higher temp, creative) ↓ polished proseA single “do everything” agent usually produces mediocre results at every step. Specialists produce better output at each stage.
The Pattern
Orchestration is regular Ruby code — no framework or DSL required:
class ResearchAgent < RubyLLM::Agent model "gpt-4o" temperature 0.2 instructions "You are a research assistant. Provide key facts and context."end
class WriterAgent < RubyLLM::Agent model "gpt-4o" temperature 0.7 instructions "You transform research notes into clear, engaging prose."end
# Orchestration — just Rubytopic = "How Ruby's garbage collector works"
researcher = ResearchAgent.newresearch = researcher.ask("Research this topic: #{topic}").content
writer = WriterAgent.newarticle = writer.ask("Write a blog post from these notes:\n\n#{research}").content
puts articleEach agent is a separate object with its own conversation history. The output of one becomes the input of the next.
Your Task
Open sequential.rb. Two agents are defined — ResearchAgent and WriterAgent. Implement the workflow:
- Instantiate
ResearchAgentand ask it to research the given topic - Pass the research output to
WriterAgentas the basis for a blog post - Print both outputs — research notes first, then the polished article
TOPIC = "Ruby's object model and why everything is an object"
puts "--- Phase 1: Research ---"researcher = ResearchAgent.newresearch = researcher.ask("Research this topic thoroughly: #{TOPIC}")puts research.contentputs
puts "--- Phase 2: Writing ---"writer = WriterAgent.newarticle = writer.ask( "Transform these research notes into a concise 2-paragraph blog post:\n\n#{research.content}")puts article.contentRun it:
$ ruby agentic_workflows/sequential.rbAdding More Stages
Extend the pipeline by adding agents:
class EditorAgent < RubyLLM::Agent model "gpt-4o" temperature 0.1 instructions <<~PROMPT You are a technical editor. Review this blog post for: - Technical accuracy - Grammar and clarity - Appropriate length (under 300 words) Return the improved version directly. PROMPTend
# Three-stage pipelineresearch = ResearchAgent.new.ask("Research: #{topic}").contentdraft = WriterAgent.new.ask("Write from:\n#{research}").contentfinal = EditorAgent.new.ask("Edit this:\n#{draft}").contentKeeping Context Between Stages
Sometimes you want one agent to accumulate context across stages. Use the same instance:
analyst = AnalystAgent.new
# Feed data in multiple messages%w[sales_q1 sales_q2 sales_q3].each do |file| analyst.ask("Here's #{file} data: #{load(file)}")end
# Final synthesis — agent has all three quarters in contextsummary = analyst.ask("Now summarize the year-to-date trends").contentThis is different from chaining — here a single agent builds up knowledge across messages.
Error Handling in Pipelines
Wrap stages in begin/rescue so one failure doesn’t kill the whole pipeline:
begin research = ResearchAgent.new.ask("Research: #{topic}").contentrescue RubyLLM::Error => e puts "Research failed: #{e.message}" research = "Research unavailable — write from general knowledge."end
article = WriterAgent.new.ask("Write from:\n#{research}").content- Preparing Ruby runtime