nimgent/agent
This page is generated from the module’s exported API and ## documentation comments.
Reusable agent configuration and execution API.
This layer owns the generic model -> tool -> model run. Applications that need persistence, compaction, permissions, or presentation can keep those concerns outside the agent and use the lower-level nimgent APIs directly.
The model and defaults are immutable by convention after construction.
Agent = ref object model*: LanguageModel instructions*: string tools*: seq[Tool] maxTokens*: int generationOptions*: GenerationOptions maxRetries*: int ## Maximum number of model turns. The agent defaults to a small bounded ## loop so an accidental tool cycle cannot run forever. maxSteps*: int toolChoice*: ToolChoice providerOptions*: ProviderOptions approvalPolicy*: ToolApprovalPolicyProcedures
Section titled “Procedures”newAgent
Section titled “newAgent”Create a reusable agent. The returned agent is configuration; each run receives its own request and response state.
proc newAgent(model: LanguageModel; instructions = ""; tools: seq[Tool] = @[]; maxTokens = 0; generationOptions = GenerationOptions(); maxRetries = 2; maxSteps = 8; providerOptions = ProviderOptions(); toolChoice = toolChoiceAuto(); approvalPolicy: ToolApprovalPolicy = nil): Agent {. raises: [ProviderError], tags: [], forbids: [].}Returns: Agent.
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
model |
LanguageModel |
|
instructions |
inferred |
"" |
tools |
seq[Tool] |
@[] |
maxTokens |
inferred |
0 |
generationOptions |
inferred |
GenerationOptions() |
maxRetries |
inferred |
2 |
maxSteps |
inferred |
8 |
providerOptions |
inferred |
ProviderOptions() |
toolChoice |
inferred |
toolChoiceAuto() |
approvalPolicy |
ToolApprovalPolicy |
nil |
runAsync
Section titled “runAsync”Run until the model finishes, no executable tool calls remain, or
maxSteps is reached.
proc runAsync(agent: Agent; prompt = ""; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): Future[ ProviderResponse] {.stackTrace: false, raises: [Exception, ValueError, ProviderError, KeyError, CatchableError], tags: [RootEffect, TimeEffect], forbids: [].}Returns: Future[ProviderResponse].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
inferred |
"" |
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
Blocking convenience wrapper around runAsync.
proc run(agent: Agent; prompt = ""; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): ProviderResponse {.raises: [ ValueError, Exception, OSError, ProviderError, KeyError, CatchableError], tags: [TimeEffect, RootEffect], forbids: [].}Returns: ProviderResponse.
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
inferred |
"" |
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
streamAsync (overload 1)
Section titled “streamAsync (overload 1)”Stream an agent run. onEvent receives normalized model deltas and may
return false to cancel the run.
proc streamAsync(agent: Agent; prompt: string; onEvent: StreamCallback; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): Future[ProviderResponse] {. stackTrace: false, raises: [Exception, ValueError, ProviderError, KeyError, CatchableError], tags: [RootEffect, TimeEffect], forbids: [].}Returns: Future[ProviderResponse].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
string |
|
onEvent |
StreamCallback |
|
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
stream (overload 1)
Section titled “stream (overload 1)”Blocking convenience wrapper around streamAsync.
proc stream(agent: Agent; prompt: string; onEvent: StreamCallback; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): ProviderResponse {.raises: [ ValueError, Exception, OSError, ProviderError, KeyError, CatchableError], tags: [TimeEffect, RootEffect], forbids: [].}Returns: ProviderResponse.
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
string |
|
onEvent |
StreamCallback |
|
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
runEventsAsync
Section titled “runEventsAsync”Run an agent while receiving lifecycle events.
proc runEventsAsync(agent: Agent; prompt = ""; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""; onEvent: AgentEventCallback = nil): Future[ProviderResponse] {. stackTrace: false, raises: [Exception, ValueError, ProviderError, KeyError, CatchableError], tags: [RootEffect, TimeEffect], forbids: [].}Returns: Future[ProviderResponse].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
inferred |
"" |
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
onEvent |
AgentEventCallback |
nil |
streamAsync (overload 2)
Section titled “streamAsync (overload 2)”Stream an agent run while receiving lifecycle events.
proc streamAsync(agent: Agent; prompt: string; onEvent: AgentEventCallback; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): Future[ProviderResponse] {. stackTrace: false, raises: [Exception, ValueError, ProviderError, KeyError, CatchableError], tags: [RootEffect, TimeEffect], forbids: [].}Returns: Future[ProviderResponse].
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
string |
|
onEvent |
AgentEventCallback |
|
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
stream (overload 2)
Section titled “stream (overload 2)”Blocking convenience wrapper around the event-aware stream.
proc stream(agent: Agent; prompt: string; onEvent: AgentEventCallback; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): ProviderResponse {.raises: [ ValueError, Exception, OSError, ProviderError, KeyError, CatchableError], tags: [TimeEffect, RootEffect], forbids: [].}Returns: ProviderResponse.
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
string |
|
onEvent |
AgentEventCallback |
|
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |
events
Section titled “events”Start a pull-based event stream for an agent run.
proc events(agent: Agent; prompt: string; messages: seq[Message] = @[]; abort: AbortCheck = nil; callbacks = RunCallbacks(); conversationId = ""; metadata: JsonNode = nil; turnId = ""): AgentEventStream {. raises: [ProviderError, Exception, ValueError], tags: [RootEffect], forbids: [].}Returns: AgentEventStream.
Arguments
Section titled “Arguments”| Name | Type | Default |
|---|---|---|
agent |
Agent |
|
prompt |
string |
|
messages |
seq[Message] |
@[] |
abort |
AbortCheck |
nil |
callbacks |
inferred |
RunCallbacks() |
conversationId |
inferred |
"" |
metadata |
JsonNode |
nil |
turnId |
inferred |
"" |