pub struct AgentCoreApp { /* private fields */ }Expand description
A Rig agent served on AgentCore Runtime’s HTTP protocol contract.
Implementations§
Source§impl AgentCoreApp
impl AgentCoreApp
Sourcepub fn max_sessions(self, max: usize) -> Self
pub fn max_sessions(self, max: usize) -> Self
Keep at most max sessions’ conversations in memory, forgetting the
oldest. This is the default, with DEFAULT_MAX_SESSIONS. It replaces
a store set with history_store.
Sourcepub fn history_store<S>(self, store: S) -> Self
pub fn history_store<S>(self, store: S) -> Self
Keep sessions in store, one session log per session id, so they
survive restarts and several apps can share them: any rig_memory
store, such as rig_memory::DirStore (feature fs) or the
rig-sqlite and rig-postgres stores. Any session id works: one that
is 1 to 128 ASCII letters, digits, - or _ names its log as it is,
any other is hashed into a safe log name. Two apps, or concurrent
invocations of one session, saving at once do not lose either turn: a
save that loses a race rereads and retries up to 10 times with a short
jittered backoff. If a save still fails, the invocation’s answer is
returned anyway, the failure is logged, and that run’s turn is not
persisted, so later invocations do not see it. A load that fails is
reported to the caller as an error, before the agent runs. The store
keeps each session’s whole conversation;
history_window limits only what an invocation
sends. Replaces the in-memory store set by max_sessions.
Sourcepub fn history_window(self, window: impl Recent + 'static) -> Self
pub fn history_window(self, window: impl Recent + 'static) -> Self
Which newest messages of a session each invocation sends the model,
such as rig_memory::recent::TokenBudget::new(8_000) or
LastMessages(20). Defaults to DEFAULT_HISTORY_TOKENS tokens. A
tool result is never sent without its call. An empty window
(TokenBudget::new(0), LastMessages(0)) sends no history.
With summarize_history, the summary is
sent first, and the window limits only the messages after it.
What the window also does depends on the store. In memory, messages
beyond it are forgotten when a run is saved, so memory stays bounded.
With history_store, the store keeps the whole
conversation and the window only limits what is sent, so a later,
wider window sees older turns again.
Sourcepub fn summarize_history(
self,
summarizer: Agent,
keep_recent: usize,
trigger_messages: usize,
) -> Self
pub fn summarize_history( self, summarizer: Agent, keep_recent: usize, trigger_messages: usize, ) -> Self
Summarize a session’s older history with summarizer once the session
holds more than trigger_messages messages. The messages before the
newest keep_recent are replaced by one summary, which becomes the
session’s first message: a user message in rig-memory’s compaction frame
(“The conversation history before this point was compacted into the
following summary: …
”). The newest messages stay
verbatim, and the cut never separates a tool call from its result.
The summary is always sent first, before the messages the window keeps,
even when the window is narrower than the summary and the kept messages.
In a session this option summarizes, a first message that opens with
rig-memory’s summary frame text is treated as the summary, even if a
caller wrote it.
summarizer should be an agent without tools: a tool call in its reply
is not used. trigger_messages is raised to keep_recent + 1 when it is
not larger, since a cut needs something older than the kept messages.
Summarizing runs before the invocation answers. Two concurrent invocations of one session may both summarize. A summary whose session changed under it (a concurrent summary, or a refused save to the store) is skipped and the history is kept, not lost.
If a summary fails (the summarizer errors or returns no text, or the
store refuses the change), a warning is logged, the session keeps its
history, and the invocation answers as usual. The next invocation tries
again. Without a store, a session whose summaries keep failing keeps at
least its newest 2 * trigger_messages messages, so memory stays
bounded; older messages are dropped. With
history_store, the store keeps the whole
conversation as it does without summaries.
Without a store, the summary is kept in memory with the session. With
history_store, the summary is written to the
store’s log in place of the messages it replaces, which the log keeps in
its history.
Each attempt is traced as an agentcore.summarize span under the
invocation span. It records the messages summarized and kept, the
outcome (summarized, skipped_changed, skipped_nothing_older or
failed), and the summarizer’s token usage when the provider reports
it. The prompt and the summary text are recorded only with
record_content, as the other GenAI content is.
Sourcepub fn record_content(self, record: bool) -> Self
pub fn record_content(self, record: bool) -> Self
Record prompts and replies on the GenAI spans. AgentCore Evaluations needs them to judge a session; they are off by default, as in Rig, because they put conversation content into your telemetry.
This setting always overrides the agent’s own
AgentBuilder::record_content_telemetry. The app passes its value to
every invocation, so the agent’s setting is ignored when the agent is
served through AgentCoreApp. To record content, call
.record_content(true) on the app. The default here is false, so an
agent built with record_content_telemetry(true) still records nothing
unless the app sets it too. This is planned to change in 0.48 (PR #42),
so that an app setting left unset keeps the agent’s.
Sourcepub fn max_concurrent_tasks(self, max: usize) -> Self
pub fn max_concurrent_tasks(self, max: usize) -> Self
Run at most max invocations at once (at least one). An invocation
beyond that gets 429 Too Many Requests before the agent runs; 429 is the
conventional throttle status. Without this there is no cap, which is the
default: AgentCore Runtime gives each session its own microVM. Outside
AgentCore Runtime, a cap around 32 suits most single-process apps.
Sourcepub fn max_run_duration(self, max: Duration) -> Self
pub fn max_run_duration(self, max: Duration) -> Self
Stop an invocation that runs longer than max, counting the summarizing
step that can run before it. A stopped invocation answers 504 Gateway
Timeout (or an {"error"} event when streaming), and its turn is not
added to the session. Without this there is no limit, which is the
default. Outside AgentCore Runtime, ten minutes suits most agents.
Sourcepub fn max_request_bytes(self, bytes: usize) -> Self
pub fn max_request_bytes(self, bytes: usize) -> Self
Refuse an /invocations body larger than bytes with 413 Payload Too
Large, before it is parsed. Defaults to DEFAULT_MAX_REQUEST_BYTES.
Sourcepub fn track_task(&self) -> BusyGuard
pub fn track_task(&self) -> BusyGuard
Mark background work as open until the returned guard drops. While any
guard is alive, /ping answers HealthyBusy, so AgentCore Runtime keeps
the session running; once they are all dropped it answers Healthy
again. Use it for work that outlives an invocation, such as a task the
app starts on its own.
Sourcepub fn spawn_task<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
pub fn spawn_task<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
Run future on the Tokio runtime as background work. /ping answers
HealthyBusy until it finishes, or until its task is aborted. Dropping
the returned handle does not stop the task. Call it on a running Tokio
runtime, as for tokio::spawn.
app.spawn_task(async {
// Work that outlives the invocation that started it.
});Sourcepub fn router(self) -> Router
pub fn router(self) -> Router
The contract’s routes, /invocations, /ping and the WebSocket /ws,
as an Axum router.
Sourcepub async fn serve(self) -> Result<(), ServeError>
pub async fn serve(self) -> Result<(), ServeError>
Serve on 0.0.0.0 at PORT (default 8080, what AgentCore sends to)
until the process gets Ctrl-C or SIGTERM. A PORT that is set but is
not a port number logs a warning and serves on the default.