Build stateful agents that maintain persistent memory across interactions
While each Modus SDK offers similar capabilities, the APIs and usage may vary between languages.
Modus Agents APIs documentation is available on the following pages:
The Modus Agents APIs allow you to create stateful agents that maintain persistent memory across interactions, survive system failures, and coordinate complex multi-step operations.
To begin, import the agents
package from the SDK:
The APIs in the agents
package are below, organized by category.
We’re constantly introducing new APIs through ongoing development with early users. Please open an issue if you have ideas on what would make Modus even more powerful for your next app!
Register an agent struct with the Modus runtime before it can be instantiated.
A pointer to an instance of the agent struct that implements the Agent
interface.
Agent registration must be done in an init()
function to ensure it happens
before any agent operations.
Create and start a new agent instance.
The name of the agent to instantiate. This must match the Name()
method
returned by the agent implementation.
Stop an agent instance. Once stopped, the agent can’t be resumed.
The unique identifier of the agent instance to stop.
Get information about a specific agent instance.
The unique identifier of the agent instance.
List all active agent instances.
Send a synchronous message to an agent and wait for a response.
The unique identifier of the target agent instance.
The name of the message to send to the agent.
Optional message options. Use WithData(data string)
to include a data
payload.
Send an asynchronous message to an agent without waiting for a response.
The unique identifier of the target agent instance.
The name of the message to send to the agent.
Optional message options. Use WithData(data string)
to include a data
payload.
Create a message option that includes a data payload.
The data payload to include with the message.
The interface that all agents must implement.
Must return a unique name for the agent implementation.
Handles incoming messages to the agent. Must be implemented by all agents.
Returns the agent’s current state as a string for persistence. Called automatically when the agent needs to be suspended or migrated.
Restores the agent’s state from a string. Called automatically when the agent is resumed or migrated.
Lifecycle method called when the agent is first created.
Lifecycle method called when the agent is about to be suspended.
Lifecycle method called when the agent is resumed from suspension.
Lifecycle method called when the agent is about to be terminated.
A convenient base struct that provides default implementations for optional methods and includes event publishing capabilities.
You can embed AgentBase
in your agent struct to automatically implement the
optional methods, then override only the ones you need.
Publishes an event from this agent to any subscribers. The event must
implement the AgentEvent
interface.
Interface that custom events must implement to be published from agents.
Must return the name/type of the event as a string.
Custom events should implement this interface and include any additional data as struct fields:
Information about an agent instance.
The unique identifier of the agent instance.
The name of the agent implementation.
The current status of the agent instance.
Options for customizing agent messages.
Here’s a complete example of a simple counter agent with event streaming:
Once deployed, your agent functions become available via GraphQL:
To receive real-time events from your agent, subscribe using GraphQL subscriptions over Server-Sent Events:
Example events you might receive:
You can define multiple event types for different scenarios:
Use appropriate GraphQL Server-Sent Events (SSE) clients such as:
Example with curl:
Example with Go client:
Build stateful agents that maintain persistent memory across interactions
While each Modus SDK offers similar capabilities, the APIs and usage may vary between languages.
Modus Agents APIs documentation is available on the following pages:
The Modus Agents APIs allow you to create stateful agents that maintain persistent memory across interactions, survive system failures, and coordinate complex multi-step operations.
To begin, import the agents
package from the SDK:
The APIs in the agents
package are below, organized by category.
We’re constantly introducing new APIs through ongoing development with early users. Please open an issue if you have ideas on what would make Modus even more powerful for your next app!
Register an agent struct with the Modus runtime before it can be instantiated.
A pointer to an instance of the agent struct that implements the Agent
interface.
Agent registration must be done in an init()
function to ensure it happens
before any agent operations.
Create and start a new agent instance.
The name of the agent to instantiate. This must match the Name()
method
returned by the agent implementation.
Stop an agent instance. Once stopped, the agent can’t be resumed.
The unique identifier of the agent instance to stop.
Get information about a specific agent instance.
The unique identifier of the agent instance.
List all active agent instances.
Send a synchronous message to an agent and wait for a response.
The unique identifier of the target agent instance.
The name of the message to send to the agent.
Optional message options. Use WithData(data string)
to include a data
payload.
Send an asynchronous message to an agent without waiting for a response.
The unique identifier of the target agent instance.
The name of the message to send to the agent.
Optional message options. Use WithData(data string)
to include a data
payload.
Create a message option that includes a data payload.
The data payload to include with the message.
The interface that all agents must implement.
Must return a unique name for the agent implementation.
Handles incoming messages to the agent. Must be implemented by all agents.
Returns the agent’s current state as a string for persistence. Called automatically when the agent needs to be suspended or migrated.
Restores the agent’s state from a string. Called automatically when the agent is resumed or migrated.
Lifecycle method called when the agent is first created.
Lifecycle method called when the agent is about to be suspended.
Lifecycle method called when the agent is resumed from suspension.
Lifecycle method called when the agent is about to be terminated.
A convenient base struct that provides default implementations for optional methods and includes event publishing capabilities.
You can embed AgentBase
in your agent struct to automatically implement the
optional methods, then override only the ones you need.
Publishes an event from this agent to any subscribers. The event must
implement the AgentEvent
interface.
Interface that custom events must implement to be published from agents.
Must return the name/type of the event as a string.
Custom events should implement this interface and include any additional data as struct fields:
Information about an agent instance.
The unique identifier of the agent instance.
The name of the agent implementation.
The current status of the agent instance.
Options for customizing agent messages.
Here’s a complete example of a simple counter agent with event streaming:
Once deployed, your agent functions become available via GraphQL:
To receive real-time events from your agent, subscribe using GraphQL subscriptions over Server-Sent Events:
Example events you might receive:
You can define multiple event types for different scenarios:
Use appropriate GraphQL Server-Sent Events (SSE) clients such as:
Example with curl:
Example with Go client: