Inspiration
Every general chatbot forgets you the moment a chat ends. For casual questions that's fine — but for investing it's a real problem. If I've already told an assistant that I only invest in Shariah-compliant stocks, that I'm a conservative long-term investor, and that I'd like answers in Urdu, I shouldn't have to repeat all of that every single time I open a new chat.
That gap is exactly what Track 1 (MemoryAgent) is about, so I built Hikmat PSX around one idea: an investing assistant for the Pakistan Stock Exchange (PSX) that actually remembers who you are across sessions — and, more importantly, uses that memory to change the advice it gives, not just the way it phrases things.
What it does
Hikmat PSX answers questions about PSX-listed companies — financials, ratios, comparisons, charts, and Shariah compliance — while quietly learning your durable preferences from ordinary conversation. Tell it once "I only want Shariah-compliant stocks," and in a completely new chat days later it will automatically flag a conventional bank as non-compliant and steer you toward a compliant alternative. The memory is visible, not hidden: a live feed shows what it remembered or forgot and why, and every answer reports how many memories it recalled for that question.
How I built it
The core is a LangGraph multi-agent graph. A router classifies each question and fans out to a finance agent (financial summaries, charts) and/or a compliance agent (SQL over PSX data); a synthesize step merges their answers, and a final reflection step maintains memory.
Memory is split into three layers with distinct lifetimes:
- Chat history — the Q&A shown in the UI, per conversation.
- Per-session working memory — a LangGraph Postgres checkpointer holding one conversation's state, trimmed with a sliding window so context stays small.
- Long-term memory — durable user preferences, keyed by user ID so they survive new chats, logout, and new sessions. This is what makes it a MemoryAgent.
The system manages that memory three ways:
- Remembering happens after each turn in a dedicated reflection node, never mid-answer, so it adds no latency. Ordinary questions store nothing, which keeps memory precise.
- Recalling avoids dumping every memory into every prompt. Hard constraints (language, the Shariah filter) are always applied, and everything else is ranked by semantic similarity to the current question using Qwen/DashScope embeddings — only the top matches are injected.
Formally, for a query $q$ and stored memories $m_i$, I compute cosine similarity:
$$ \mathrm{sim}(q, m_i)= \frac{q \cdot m_i} {|q|\,|m_i|} $$
Only the top-$k$ most relevant memories are injected into the prompt, keeping the context window small even as memory grows.
- Forgetting works at two timescales: within a session the oldest turns are trimmed away, and across sessions preferences are deleted when the user revokes them — plus the user can delete chats and clear stored preferences directly.
Everything runs on Qwen via Alibaba Cloud DashScope (both chat and embeddings), deployed as a Docker Compose stack — PostgreSQL, a FastAPI backend with Server-Sent Events for streaming, and an nginx-served React frontend — on an Alibaba Cloud ECS instance.
Challenges I ran into
Keeping semantic memory retrieval fast. My first implementation semantically searched every stored memory for each user query, which introduced noticeable latency. I redesigned the system by splitting long-term memory into two groups: essential cross-session preferences such as language and the Shariah filter, which are always injected into the prompt, and semantic memories, which are retrieved only when they exceed a similarity threshold for the current query. This significantly reduced latency while preserving personalization.
Writing memories without delaying responses. Initially, memory updates were performed by the agent itself through tool calls before completing the response. Although functional, this increased user-perceived latency. I redesigned the workflow to stream responses from the multi-agent system immediately, while a dedicated Memory Writer node executes asynchronously after the agents finish. Users receive fast responses, and durable memory is updated in the background.
Preventing prompt growth. As more user preferences accumulated, injecting every stored preference into the system prompt made prompts unnecessarily large. I solved this by always including only critical preferences while retrieving the remaining memories semantically when they are relevant. This keeps the context window compact and scalable as memory grows.
Accomplishments that I'm proud of
Building a MemoryAgent that genuinely personalizes investment advice across completely new sessions instead of simply remembering conversation history.
Designing a memory architecture that supports remembering, semantic recall, updating, and forgetting while keeping every memory operation visible through a live memory feed and recall indicators.
Achieving low-latency streaming responses from a LangGraph multi-agent architecture by decoupling memory writing from the response generation pipeline.
Successfully deploying the complete application on Alibaba Cloud ECS using Qwen models through DashScope, LangGraph, FastAPI, PostgreSQL, React, and Docker Compose.
Creating a practical AI assistant for Pakistan Stock Exchange investors that makes investing more accessible through personalized, memory-driven guidance.
What I learned
The interesting problem in a MemoryAgent isn't storing data — it's curation: deciding what is worth remembering, recalling only what's relevant, and forgetting on time. Making memory visible and user-controllable turned out to matter as much as the retrieval itself; watching the agent remember, recall, and forget is what makes it feel like it actually knows you.
I also learned that performance is just as important as correctness. Separating response generation from memory writing and carefully deciding which memories belong in the prompt allowed me to build an assistant that feels both intelligent and responsive.
What's next for Hikmat PSX
- Proactive memory — a personalized daily market brief generated from your stored preferences.
- Expand the Shariah-compliance dataset to cover the entire Pakistan Stock Exchange.
- Continue improving memory reflection so the assistant becomes even better at distinguishing durable preferences from one-off requests. ```
Built With
- alibaba-cloud
- dashscope
- docker
- fastapi
- javascript
- langchain
- langgraph
- matplotlib
- nginx
- postgresql
- python
- qwen
- react
- seaborn
- server-sent-events
- sql
- vite
Log in or sign up for Devpost to join the conversation.