Generation
Execute AI generations synchronously or asynchronously using ActiveAgent's generation methods.
Synchronous
Execute immediately and return the response:
ApplicationAgent.prompt(message: "Hello").generate_nowUse prompt_now (alias: generate_now) for generations or embed_now for embeddings.
Asynchronous
Queue for background execution using Active Job:
ApplicationAgent.prompt(message: "Analyze this data").generate_laterApplicationAgent.prompt(message: "Generate report").generate_later(
queue: :reports,
priority: 10,
wait: 5.minutes
)Use prompt_later (alias: generate_later) for generations or embed_later for embeddings. Background jobs run through ActiveAgent::GenerationJob and respect your Active Job configuration.
Configure queue name and adapter:
class MyAgent < ApplicationAgent
self.generate_later_queue_name = :ai_tasks
endJobs use your Active Job adapter (Sidekiq, Resque, etc.).
Interfaces
Direct Generation
Generate without defining action methods:
response = ApplicationAgent.prompt(
"What is 2+2?",
temperature: 0.7
).generate_nowParameterized Generation
Pass parameters to action methods:
class WelcomeAgent < ApplicationAgent
def greet
prompt(message: "Hello #{params[:name]}!")
end
endresponse = WelcomeAgent.with(name: "Alice").greet.generate_nowAction-Based Generation
Define reusable actions:
class SupportAgent < ApplicationAgent
def help(topic)
prompt(message: "Help with #{topic}")
end
endresponse = SupportAgent.help("authentication").generate_nowInspecting Before Execution
Access prompt properties before generating:
generation = ApplicationAgent.prompt(message: "test")This is useful for debugging, testing, or conditional execution.
Prompt Previews
Preview the final prompt without executing generation, including rendered templates and merged parameters:
preview = ApplicationAgent.prompt(
instructions: "You are a helpful assistant",
message: "What is 2+2?",
temperature: 0.7
).prompt_previewThe preview returns markdown-formatted output with YAML frontmatter:
Markdown Preview
activeagent/test/docs/agents/generation_examples_test.rb:200
---
:temperature: 0.7
---
## Instructions
You are a helpful assistant
## Messages
### Message 1 (User)
What is 2+2?TIP
Since previews include all parameters, messages, instructions, and tool definitions, hashing the preview can be used to build cache keys.
Response Objects
All generations return response objects:
response = ApplicationAgent.prompt(message: "Hello").generate_nowresponse.message # Most Recent Message
response.messages # Full Message Stack
response.parsed_json # Extracted JSON, if Parsable
response.raw_request # The most recent request in provider format
response.raw_response # The most recent response in provider format
response.context # The original context that was sent
# Usage statistics (see /actions/usage for details)
response.usage # Normalized usage object across all providers
response.usage.input_tokens
response.usage.output_tokens
response.usage.total_tokensFor embeddings:
response = ApplicationAgent.embed(input: "text").embed_nowresponse.data # Collection of Embedding Datum
response.raw_request # The most recent request in provider format
response.raw_response # The most recent response in provider format
response.context # The original context that was sent
# Usage statistics
response.usage # Normalized usage object
response.usage.input_tokensNext Steps
- Agents - Understanding the full agent lifecycle
- Actions - Define what your agents can do
- Usage Statistics - Track token consumption and costs
- Messages - Work with multimodal content
- Tools - Enable function calling capabilities
- Streaming - Stream responses in real-time
- Callbacks - Hook into generation lifecycle
- Error Handling - Handle failures gracefully
- Configuration - Configure generation behavior
- Testing - Test generation patterns