LiteLLM Append Trailing User Message
Append a trailing user turn so Claude 4.6+ models that reject assistant prefill work through LiteLLM.
Claude 4.6+ does not support assistant message prefill. The LiteLLM formatter appends a user turn when the final input message has the assistant role.
The source below supplies ordinary user messages and never exercises the append operation. Its separate reasoning_model stages are skipped because Agno does not recognize the LiteLLM wrapper as a supported native reasoning adapter. Run the current example below to exercise the formatter directly through an agent.
"""
LiteLLM Append Trailing User Message
=====================================
Claude 4.6+ does not support assistant message prefill. Enable
`append_trailing_user_message` to append a trailing user turn when the
conversation ends with an assistant message (e.g. during reasoning).
Use `trailing_user_message_content` to customise the appended text (defaults to "continue").
Note: Claude 4.6+ models auto-detect and enable this flag automatically.
"""
from agno.agent import Agent
from agno.models.litellm import LiteLLM
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=LiteLLM(
id="anthropic/claude-sonnet-4-6",
# Claude 4.6 rejects temperature + top_p together; drop top_p.
top_p=None,
append_trailing_user_message=True,
),
reasoning_model=LiteLLM(id="anthropic/claude-opus-4-7", top_p=None),
markdown=True,
)
# With custom trailing content
agent_custom = Agent(
model=LiteLLM(
id="anthropic/claude-sonnet-4-6",
top_p=None,
append_trailing_user_message=True,
trailing_user_message_content="continue",
),
reasoning_model=LiteLLM(id="anthropic/claude-opus-4-7", top_p=None),
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent.print_response("What is 15 + 27?")
agent_custom.print_response("What is 15 + 27?")Current Example
Each request ends with an assistant input message. The formatter appends continue on the first run and . on the second; this does not invoke a separate reasoning model.
from agno.agent import Agent
from agno.models.litellm import LiteLLM
from agno.models.message import Message
for trailing_text in ("continue", "."):
agent = Agent(
model=LiteLLM(
id="anthropic/claude-sonnet-4-6",
top_p=None,
append_trailing_user_message=True,
trailing_user_message_content=trailing_text,
),
markdown=True,
)
agent.print_response([
Message(role="user", content="What is 15 + 27?"),
Message(role="assistant", content="I will add the two numbers."),
])Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno litellmExport your Anthropic API key
unset LITELLM_API_KEY
export ANTHROPIC_API_KEY="your_anthropic_api_key_here"Run the example
Save the Current Example as trailing_current.py, then run:
python trailing_current.pyFull source: cookbook/90_models/litellm/append_trailing_user_message.py