RecursiveLanguageModelAgent module
RecursiveLanguageModelAgent
Bases: CodeModeAgent
A recursive-language-model agent.
A code-mode agent extended with two always-on recursive helpers:
llm_query(prompt) and llm_query_batched(prompts). The agent
treats long inputs as an external environment, it writes Python
that slices, filters, and aggregates the data, and recursively
delegates semantic work to a sub-LM only on the snippets it cares
about. Compared to feeding a long document straight into the
primary LM, this trades a single huge context for many small ones,
which both fits inside provider limits and reduces the chance of
long-context regressions.
State (variables, imports, function definitions) accumulates across
turns in the persistent sandbox, so the agent can build up
intermediate values, probe data, and iterate. The submit tool
is the canonical termination signal, exactly as in
:class:CodeModeAgent.
The llm_query quota is per-call: every invocation of this agent
gets a fresh budget of max_llm_calls sub-LM queries, and
concurrent invocations of the same agent instance each get an
independent budget, the counter and lock are built inside
call() and never shared across runs.
Example:
import synalinks
import asyncio
class Doc(synalinks.DataModel):
text: str
class Answer(synalinks.DataModel):
answer: str
async def main():
primary = synalinks.LanguageModel(model="openai/gpt-4o")
cheap = synalinks.LanguageModel(model="openai/gpt-4o-mini")
inputs = synalinks.Input(data_model=Doc)
outputs = await synalinks.RecursiveLanguageModelAgent(
data_model=Answer,
language_model=primary,
sub_language_model=cheap,
max_iterations=8,
max_llm_calls=20,
)(inputs)
agent = synalinks.Program(inputs=inputs, outputs=outputs)
long_text = open("book.txt").read()
result = await agent(Doc(text=long_text))
print(result.prettify_json())
if __name__ == "__main__":
asyncio.run(main())
References
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict
|
Optional. The target JSON schema for the final
structured answer. If not provided, use |
None
|
data_model
|
DataModel | SymbolicDataModel | JsonDataModel
|
Optional. The target data model for the final answer. |
None
|
language_model
|
LanguageModel
|
The language model driving the per-turn code generator and the final-formatting step. |
None
|
sub_language_model
|
LanguageModel
|
Optional. The language
model used by |
None
|
tools
|
list
|
Optional. Extra :class: Naming gotcha: each tool is registered under
|
None
|
prompt_template
|
str
|
Optional. Prompt template forwarded to the per-turn code generator. |
None
|
examples
|
list
|
Optional. Examples forwarded to the per-turn code generator. |
None
|
instructions
|
str
|
Optional. Instructions for the per-turn
code generator. Defaults to |
None
|
final_instructions
|
str
|
Optional. Instructions for the final
answer generator. Defaults to |
None
|
temperature
|
float
|
Optional. Sampling temperature (Default 0.0). |
0.0
|
use_inputs_schema
|
bool
|
Optional. Feed the input schema to the generator prompt (Default False). |
False
|
use_outputs_schema
|
bool
|
Optional. Feed the output schema to the generator prompt (Default False). |
False
|
reasoning_effort
|
str
|
Optional. One of |
None
|
use_chain_of_thought
|
bool
|
Optional. Wrap the per-turn
generator in ChainOfThought so it emits a |
False
|
autonomous
|
bool
|
Optional. If |
True
|
timeout
|
int
|
Per-turn execution budget in seconds
(Default 60). Higher than the parent's 5s default because
recursive sub-LM calls dominate per-turn wall time —
|
60
|
max_iterations
|
int
|
Maximum number of code-execution turns before forcing the final answer step (Default 20). Higher than the parent's 5 because RLM workflows characteristically explore → carve → batch-query → aggregate → submit, which needs more turns than plain code-mode reasoning. |
20
|
max_llm_calls
|
int
|
Hard cap on sub-LM calls per agent
invocation, shared between |
50
|
max_output_chars
|
int
|
Maximum characters to include from
REPL output in the per-turn observation (Default 10_000).
Anything beyond is truncated with a
|
10000
|
return_inputs_with_trajectory
|
bool
|
Optional. Whether to
return the full trajectory alongside the final answer
(Default |
True
|
sandbox_type
|
type
|
Optional. The |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
Source code in synalinks/src/modules/agents/recursive_language_model_agent.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
get_default_instructions()
Default instructions for the recursive-language-model agent.