SShortSingh.
Back to feed

How PHP Sessions Enable Stateful AI Chat Without Persistent Memory

0
·1 views

Building a stateful chatbot in PHP is complicated by the fact that each HTTP request runs as a fresh process with no shared memory between calls. A practical solution uses PHP sessions via session_start(), storing conversation history in the $_SESSION array so it persists across requests for the same browser. The user's message and the AI agent's reply are each appended to this session array, which is passed to the agent on every request to maintain conversational context. For more durable storage, a one-line swap replaces session-based history with a FileMemory or PdoMemory driver, enabling persistence across server restarts and support for multiple users. This approach is part of the NanoAgent examples series, which demonstrates modular AI agent patterns in PHP.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Brazilian Lab Trains 144M-Parameter Decision AI Model for Just $104

Supersonic Labs, a small AI lab in Brazil, has released Julia 1, a 144.3-million-parameter decision model trained at a total cost of approximately US$104. Unlike generative language models, Julia 1 does not produce text — instead, it accepts a context, a question, and up to 20 options, then returns ranked scores for each choice. The model runs on consumer hardware including Android tablets and budget laptop CPUs, with weights of 550 MB fitting within 400 MB of RAM. In benchmark testing, Julia 1 outperformed comparable models on emotion classification by 38 percentage points, though it struggled with large label sets such as Banking77's 72 categories. Released on Hugging Face under the Apache 2.0 license, the model also achieved over 71% accuracy across 52 language locales in multilingual scenario classification.

0
ProgrammingDEV Community ·

How PayEcho Forced Its LLM to Act on Customer History, Not Ignore It

Engineers at PayEcho, a payment-recovery and credit-decision platform, found that integrating recalled customer history into an LLM's context was not enough to change its recommendations. The model consistently produced generic advice even when months of behavioral data were available, treating historical information as background rather than evidence. The team solved this by requiring the model to explicitly cite specific past outcomes — such as a customer ignoring emails but responding to WhatsApp — as justification for each recommendation. Retrieval and generation were kept as separate pipeline stages, making it easier to diagnose whether a generic output stemmed from poor memory recall or failure to reason over retrieved data. Actual outcomes from each interaction are written back to memory, creating a feedback loop where past results become evidence for future decisions.

0
ProgrammingDEV Community ·

How to Stream LLM Tokens in PHP Using Server-Sent Events

Developers building chat interfaces in PHP can eliminate the blank-screen wait by streaming large language model (LLM) tokens incrementally using Server-Sent Events (SSE). The NanoAgent library's Agent::stream() method delivers each token via a callback, which the developer formats as an SSE data line and immediately flushes to the browser. Four specific HTTP headers are required to convert a standard PHP response into a persistent SSE stream, with the X-Accel-Buffering header being critical for Nginx environments that buffer upstream responses by default. On the client side, a native browser EventSource object handles incoming tokens without any additional libraries, appending each token to the UI until a [DONE] sentinel signals the end of the stream. Compared to WebSockets, SSE is simpler for one-way token delivery since it runs over plain HTTP and supports automatic reconnection.

0
ProgrammingDEV Community ·

College Game Dev Club Builds Mobile-Friendly 3D Web Game Using Three.js

A college game development club built a browser-based 3D exploration game using Three.js, TypeScript, Cannon.es, and GLSL shaders, distributable via a simple QR code. The game features a car navigating an island with a mini-game and information zones, inspired by developer Bruno Simon's interactive portfolio. To ensure smooth performance on mobile devices, the team used instanced geometry and custom GPU shaders to render 15,000 grass blades, procedural flowers, butterflies, and dolphins while minimizing CPU load. Expensive post-processing effects like SSAO and Bloom were replaced with lightweight alternatives, including darkened grass bases to simulate ambient occlusion and CSS filters for color grading. The project was developed in three phases: establishing driving controls with primitives, integrating 3D models, and finally refining visuals while optimizing for mobile browsers.