Examples
Practical examples of using the Agentuity JavaScript SDK
This section provides practical examples of using the Agentuity JavaScript SDK for common use cases.
Basic Agent Implementation
Here's a complete example of a basic agent that processes JSON requests and returns JSON responses:
Key-Value Storage Usage
This example demonstrates how to use the key-value storage API to store and retrieve user preferences:
Vector Storage Usage
This example demonstrates how to use the vector storage API for semantic search:
Agent Communication Example
This example demonstrates how agents can communicate with each other:
Error Handling Example
This example demonstrates comprehensive error handling in an agent:
Telemetry Example
This example demonstrates how to use OpenTelemetry for tracing and metrics:
Welcome Function Example
This example demonstrates how to customize the initial appearance of DevMode when it starts interacting with your agents:
Real-World Example
Here's a more comprehensive example from the React Miami 2025 Concierge template:
Note that in this example, some prompts use plain text directly while others use base64 encoding. Both approaches work, but using base64 encoding is recommended for consistency.
OpenAI Streaming Example
This example demonstrates how to stream responses from OpenAI models in your Agentuity agent. Streaming allows for a more responsive user experience by displaying the AI's response as it's being generated, rather than waiting for the entire response to complete.
How It Works
- We import the necessary types from
@agentuity/sdk
and utilities from the Vercel AI SDK (ai
and@ai-sdk/openai
). - The
streamText
function from the Vercel AI SDK creates a streaming text response from OpenAI. - We configure the stream with the
gpt-4o
model and a prompt. - The
textStream
object contains the streaming response from OpenAI. - We return the stream using
resp.stream()
, which handles the streaming response in the Agentuity platform.
Dependencies
To use this example, you'll need to install the following dependencies:
Agent-to-Agent Streaming Example
This example demonstrates how to call another agent and stream its response back to the client. This is useful for creating agent workflows where one agent processes a request and streams the response through another agent.
How It Works
- We use
ctx.getAgent()
to get a reference to another agent named 'HistoryExpert'. - We invoke the agent with
agent.run()
, passing the data we want to process. - We retrieve the stream from the agent's response using
agentResponse.data.stream()
. - Finally, we return the stream to the client using
resp.stream()
, which passes the streaming response through our agent.
This pattern allows you to:
- Chain multiple agents together in a workflow
- Maintain a streaming experience end-to-end
- Process or transform streaming data between agents
Learn More About Streaming
For more information about agent streaming, check out these resources:
- Agents just want to have streams - Blog post explaining the importance and implementation of streaming in agents
- Streams for agents! - YouTube video demonstration of agent streaming capabilities