Skip to content

Instantly share code, notes, and snippets.

@larkintuckerllc
Last active December 31, 2025 13:19
Show Gist options
  • Select an option

  • Save larkintuckerllc/59b2b06e8edd0e125912f6bd887f4fb2 to your computer and use it in GitHub Desktop.

Select an option

Save larkintuckerllc/59b2b06e8edd0e125912f6bd887f4fb2 to your computer and use it in GitHub Desktop.
from typing import Callable
from langchain.agents import create_agent
from langchain.agents.middleware import wrap_model_call, ModelRequest, ModelResponse
from langchain.chat_models import init_chat_model
from langchain.messages import AIMessage, HumanMessage
LARGE_MODEL = init_chat_model("gpt-5-mini")
STANDARD_MODEL = init_chat_model("gpt-5-nano")
@wrap_model_call
def state_based_model(request: ModelRequest,
handler: Callable[[ModelRequest], ModelResponse]) -> ModelResponse:
message_count = len(request.messages)
if message_count > 10:
model = LARGE_MODEL
else:
model = STANDARD_MODEL
request = request.override(model=model)
return handler(request)
agent = create_agent(
model="gpt-5-nano",
middleware=[state_based_model],
system_prompt="You are roleplaying a real life helpful office intern."
)
response = agent.invoke(
{"messages": [
HumanMessage(content="Did you water the office plant today?")
]}
)
print("Short conversation:")
print(response["messages"][-1].content)
print("")
response = agent.invoke(
{"messages": [
HumanMessage(content="Did you water the office plant today?"),
AIMessage(content="Yes, I gave it a light watering this morning."),
HumanMessage(content="Has it grown much this week?"),
AIMessage(content="It's sprouted two new leaves since Monday."),
HumanMessage(content="Are the leaves still turning yellow on the edges?"),
AIMessage(content="A little, but it's looking healthier overall."),
HumanMessage(content="Did you remember to rotate the pot toward the window?"),
AIMessage(content="I rotated it a quarter turn so it gets more even light."),
HumanMessage(content="How often should we be fertilizing this plant?"),
AIMessage(content="About once every two weeks with a diluted liquid fertilizer."),
HumanMessage(content="When should we expect to have to replace the pot?")
]}
)
print("Long conversation:")
print(response["messages"][-1].content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment