Implementation:Neuml Txtai Agent Call
| Knowledge Sources | |
|---|---|
| Domains | NLP, Agent |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete tool for executing the agent reasoning loop that processes a user query through iterative LLM inference and tool invocation, provided by the txtai library.
Description
Agent.__call__ is the main entry point for running a configured agent against a user query. When an Agent instance is called as a function, this method orchestrates the full execution lifecycle: parameter configuration, optional memory reset, prompt rendering, agent loop execution, memory update, and result return.
The method performs the following steps:
- Parameter configuration: Calls self.process.model.parameters(maxlength) to set the maximum token length on the underlying PipelineModel. The default maxlength is 8192.
- Memory reset: If reset=True and memory is enabled, calls self.memory.clear() to discard all prior conversation context.
- Prompt rendering: Calls self.prompt(text) which uses a Jinja2 template to combine the user's query with formatted memory entries. The default template places the query first, then appends prior conversation history (if any) with an instruction to use or ignore it based on relevance.
- Agent loop execution: Calls self.process.run(prompt, stream=stream, **kwargs), which triggers the smolagents iterative loop. The underlying agent (CodeAgent or ToolCallingAgent) repeatedly invokes the LLM, parses tool-call actions, executes the corresponding tools, appends observations, and re-prompts until a final answer is produced or max_steps is reached.
- Memory update: If memory is enabled, appends the (text, output) tuple to self.memory, a bounded deque that automatically evicts the oldest entry when full.
- Return: Returns the final output string to the caller.
Usage
Use Agent.__call__ to execute a query against a configured agent. This is the primary runtime interface -- after constructing an Agent, you simply call it with a text string. It supports multi-turn conversations through memory, streaming for interactive UIs, and per-call sequence length configuration.
Code Reference
Source Location
- Repository: txtai
- File:
src/python/txtai/agent/base.py - Lines: 52-81
Signature
def __call__(self, text, maxlength=8192, stream=False, reset=False, **kwargs):
# Process parameters
self.process.model.parameters(maxlength)
# Clear memory, if reset flag set
if reset and self.memory:
self.memory.clear()
# Run agent loop
output = self.process.run(self.prompt(text), stream=stream, **kwargs)
# Add output to memory, if necessary
if self.memory is not None:
self.memory.append((text, output))
return output
Import
from txtai.agent import Agent
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| text | str | Yes | The user query or instructions to process. This is the main input to the agent. |
| maxlength | int | No | Maximum sequence length for the LLM. Defaults to 8192 tokens. |
| stream | bool | No | If True, the response is streamed incrementally. Defaults to False. |
| reset | bool | No | If True, clears all stored memory before processing. Defaults to False. |
| **kwargs | dict | No | Additional keyword arguments forwarded to process.run(). |
Outputs
| Name | Type | Description |
|---|---|---|
| output | str | The final answer produced by the agent after completing its reasoning loop. This is the result of the LLM's final response after all tool calls and observations have been processed. |
Usage Examples
Basic Example
from txtai.agent import Agent
agent = Agent(
model="huggingface-hub/Meta-Llama-3-8B-Instruct",
tools=["websearch"],
max_steps=5,
)
# Simple single-turn query
result = agent("What is the capital of France?")
print(result)
Multi-Turn Conversation with Memory
from txtai.agent import Agent
agent = Agent(
model="huggingface-hub/Meta-Llama-3-8B-Instruct",
tools=["websearch", "python"],
memory=5,
max_steps=8,
)
# First turn
result1 = agent("What is the population of Tokyo?")
print(result1)
# Second turn -- the agent remembers the prior exchange
result2 = agent("How does that compare to New York City?")
print(result2)
# Start a fresh conversation
result3 = agent("What is quantum computing?", reset=True)
print(result3)
Streaming Response
from txtai.agent import Agent
agent = Agent(
model="huggingface-hub/Meta-Llama-3-8B-Instruct",
tools=["websearch"],
max_steps=5,
)
# Stream the response for a real-time UI
for chunk in agent("Explain the theory of relativity", stream=True):
print(chunk, end="", flush=True)
Custom Max Length
from txtai.agent import Agent
agent = Agent(
model="huggingface-hub/Meta-Llama-3-8B-Instruct",
tools=["python"],
max_steps=10,
)
# Use a larger context window for complex tasks
result = agent(
"Write and execute a Python function that computes the first 20 Fibonacci numbers",
maxlength=16384,
)
print(result)