Output Continuation

Auto-continue a long answer cut off by the model's output-token limit

Output continuation lets an agent transparently finish a long answer the model cut off at its output-token limit.

Diagram

No

Yes

Yes

No

📝 Request

🧠 Model

✂️ Cut off?

✅ Final answer

🔁 Append partial + continue prompt

Cap left?

⚠️ length_truncated

Quick Start

  1. On by default
    python
    from praisonaiagents import Agent
    
    # Nothing to configure — the agent auto-continues up to 3 times
    # when the output-token limit truncates its answer.
    agent = Agent(
        name="Writer",
        instructions="You write long, detailed essays."
    )
    
    agent.start("Write a 3000-word essay about the history of the internet")
  2. Raise the cap
    python
    from praisonaiagents import Agent, ExecutionConfig
    
    agent = Agent(
        name="Writer",
        instructions="You write long, detailed essays.",
        execution=ExecutionConfig(output_continuation=5)
    )
    
    agent.start("Write a 3000-word essay about the history of the internet")
  3. Turn it off
    python
    from praisonaiagents import Agent, ExecutionConfig
    
    # A truncated answer surfaces `length_truncated` immediately.
    agent = Agent(
        name="Writer",
        instructions="You write long, detailed essays.",
        execution=ExecutionConfig(output_continuation=False)
    )
    
    agent.start("Write a 3000-word essay about the history of the internet")
    print(agent.last_stop_reason)
  4. Pair with a low max_tokens
    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Writer",
        instructions="You write long, detailed essays.",
        llm={"model": "gpt-4o-mini", "max_tokens": 200}
    )
    
    print(agent.start("Write a detailed essay about the Roman Empire"))
    print(agent.last_stop_reason)  # "completed" once the pieces are stitched

What the User Sees

The user gets one complete answer and never has to type “continue”.

Diagram
ModelAgentUserModelAgentUserAppends partial + continue prompt(hidden from the caller)Write a long essay about…RequestPartial answer (finish_reason=length)Continue requestRest of answer (finish_reason=stop)Full stitched answer

How It Works

The agent keeps a per-turn counter and stitches each piece onto the accumulated text.

PhaseWhat happens
1. First responseThe model stops with finish_reason == "length".
2. ClassifyOnly a length stop on a final answer triggers continuation. Content filters, refusals and normal stops never do.
3. Re-promptThe agent adds the partial assistant turn plus a fixed “continue exactly where you left off” user turn to a local copy of the conversation.
4. StitchEach new piece is appended to the earlier text; the stitched text becomes the final answer.
5. CapUp to 3 continuations by default. Past the cap, the agent returns the stitched text and last_stop_reason == "length_truncated".

When Continuation Is Skipped

Diagram

Yes

No

Yes

No

Yes

No

✂️ Length-truncated reply

Structured output?

Skip: JSON would break

Tool-call turn?

Skip: tool loop handles it

Empty text or cap used up?

Surface length_truncated

🔁 Continue

CaseWhy
output_json= / output_pydantic=Stitching text onto a JSON schema would break parsing.
response_format other than {"type": "text"}Same reason. {"type": "text"} still continues.
Tool-call turnsOnly final answers continue.
Empty partial textNothing to continue from.
output_continuation=False or 0Explicitly disabled.

Value Shapes

output_continuation is a field on the existing ExecutionConfig, not a separate Agent(...) parameter.

ValueMeaning
True (default)Up to 3 continuations.
NoneSame as True.
False or 0Disabled — length_truncated surfaces on the first truncation.
int >= 1Custom cap, e.g. output_continuation=10.

Configuration Options

OptionTypeDefaultDescription
output_continuationUnion[bool, int]TrueAuto-continue a final answer cut off by finish_reason == "length". True/None = cap of 3, False/0 = disabled, int = custom cap. Ignored for structured outputs.
ExecutionConfig API Reference

Full ExecutionConfig options


Common Patterns

Long-form writer

python
from praisonaiagents import Agent

agent = Agent(
    name="Essayist",
    instructions="You write complete essays.",
    llm={"model": "gpt-4o-mini", "max_tokens": 300}
)

essay = agent.start("Write an essay on renewable energy")

Pin the old behaviour

python
from praisonaiagents import Agent, ExecutionConfig

agent = Agent(
    name="Reporter",
    instructions="Answer in detail.",
    execution=ExecutionConfig(output_continuation=False)
)

agent.start("Explain how vaccines work in depth")
if agent.last_stop_reason == "length_truncated":
    print("Partial answer — caller decides what to do")

Structured output

python
from pydantic import BaseModel
from praisonaiagents import Agent, Task, PraisonAIAgents

class Report(BaseModel):
    title: str
    summary: str

agent = Agent(name="Analyst", instructions="Write reports.")
task = Task(
    description="Write a report on solar power",
    agent=agent,
    output_pydantic=Report
)

# Continuation is skipped automatically — raise max_tokens instead.
PraisonAIAgents(agents=[agent], tasks=[task]).start()

Best Practices

Leave it on by default

The common failure is a half-finished answer the user has to re-prompt. The default cap of 3 covers it.

Lower the cap for cost-sensitive pipelines

output_continuation=1 keeps one safety net without large extra token spend.

Pair with max_steps for predictable budgets

max_steps caps the tool-use loop; output continuation caps single-answer overflow. They are independent.

Raise max_tokens for structured output

Continuation is skipped for JSON/schema outputs, so give those calls enough max_tokens up front.

Pin the terminal outcome in tests

Set output_continuation=False to assert on last_stop_reason == "length_truncated" without extra model calls.


Related