Skip to content
Hasnain Ali

Senior AI EngineerHasnain Ali

I build AI systems that do useful work.

Voice agents, document intelligence, and the backend systems that make them work in production.

Remote from Pakistan (UTC+5). Open to relocation. US roles need visa sponsorship.

Hasnain Ali
Meet HasnainAnimated introduction37 sec

Animated with AI. Narrated in a clone of my voice.

Work

Voice AI for real-estate leads

Speculo.ai

Voice AI that calls, texts and emails real-estate leads, then hands the qualified ones to the client's human agents.

Read the architecture

It contributed to $2M+ in annual revenue.

Senior AI Communication Engineer at Speculo.ai

Work with me on voice AI

My part: the Python backend that times every call, text and follow-up.

Pub/Sub delivers at least once, so idempotency keys prevent duplicate sends.

A demo call with fictional names. Both voices are AI.

Transcript

AI assistant: Hi Leila! This is the AI assistant from Northbrook Homes.

AI assistant: So, you asked about the house on Maple Lane?

Leila: Yes! Is it still available?

AI assistant: Mm, it is! Saturday at eleven, or would two work better?

Leila: Eleven. Can you text me the details?

AI assistant: Great, done. I've sent the details. Ben will meet you there.

Follow one lead

An illustration with a fictional lead. The steps follow the published architecture; nothing here touches a real system.

  1. Lead inDone: Lead acceptedvalid, not a duplicate
  2. MessageUpdate: Draft passed the checkemail and call script
  3. Sent onceDone: Email sentkey: leila · email
  4. CallUpdate: Call answeredqualifying questions
  5. PersonDone: Handed to Benthe client's agent

Leila fictional lead

Asked about a house on Maple Lane

Not contacted yet

Normal flow

One lead from first contact to a person, the way the pipeline is meant to run.

Press Play story, or go one step at a time with Next step.

Provider unavailable

The same lead, but the email provider is down when her message is ready.

Press Play story, or go one step at a time with Next step.

Step 1 of 5

A new lead arrives

Leila asked about a house. The system checks her record, drops duplicates and scores the lead.

Step 2 of 5

A message written for her

An LLM writes her email and a call script. A quality check scores the draft, and a weak one is written again.

Step 3 of 5

Recorded, then sent

Before sending, the worker records a key for this lead and channel. If the queue delivers the message again, the key is already there and the copy is skipped.

Step 4 of 5

No reply, so the agent calls

The email gets no reply, so the voice agent calls her and works through the qualifying questions.

Step 5 of 5

Handed to a person

Leila is qualified, so the lead goes to Ben, the client's human agent, to take it from here.

Step 1 of 5

A new lead arrives

Leila asked about a house. The system checks her record, drops duplicates and scores the lead.

Step 2 of 5

A message written for her

An LLM writes her email and a call script. A quality check scores the draft, and a weak one is written again.

Step 3 of 5

The email provider refuses it

The provider is down and refuses the request before accepting it. Nothing went out, and the system knows that.

Step 4 of 5

Marked failed, back in the queue

The worker marks the send as failed, which frees the key to be claimed again, and gives the message back. Pub/Sub delivers it again later.

Step 5 of 5

The retry goes through

On redelivery the worker claims the key and sends. Leila gets one email, and the follow-up call and hand-off carry on as normal.

Finished Normal flow

Handed to a person

What the system knew
Each send was recorded before it went out, so a redelivered copy would have been skipped.
What it did
Checked, wrote, sent and followed up without anyone running the campaign by hand.
Does a person step in?
Yes, by design: a qualified lead goes to the client's human agent.
How the pipeline handles this

Finished Provider unavailable

The retry goes through

What the system knew
The provider refused the request, so nothing had gone out.
What it did
Marked the send failed and let the queue retry it, instead of dropping the lead.
Does a person step in?
Only if it keeps failing: a queue backlog that stays high raises an alert.
How the pipeline handles this

Design note: when is a retry safe?

In the failure story the provider refuses outright, so a retry is safe. A timeout is different: the provider may already have accepted the email, so resending without checking could send it twice, and that case needs a check first. This is a general point, not a description of Speculo's code.

Read both scenarios as text

Normal flow

  1. A new lead arrives. Leila asked about a house. The system checks her record, drops duplicates and scores the lead.
  2. A message written for her. An LLM writes her email and a call script. A quality check scores the draft, and a weak one is written again.
  3. Recorded, then sent. Before sending, the worker records a key for this lead and channel. If the queue delivers the message again, the key is already there and the copy is skipped.
  4. No reply, so the agent calls. The email gets no reply, so the voice agent calls her and works through the qualifying questions.
  5. Handed to a person. Leila is qualified, so the lead goes to Ben, the client's human agent, to take it from here.

Does a person step in? Yes, by design: a qualified lead goes to the client's human agent.

Provider unavailable

  1. A new lead arrives. Leila asked about a house. The system checks her record, drops duplicates and scores the lead.
  2. A message written for her. An LLM writes her email and a call script. A quality check scores the draft, and a weak one is written again.
  3. The email provider refuses it. The provider is down and refuses the request before accepting it. Nothing went out, and the system knows that.
  4. Marked failed, back in the queue. The worker marks the send as failed, which frees the key to be claimed again, and gives the message back. Pub/Sub delivers it again later.
  5. The retry goes through. On redelivery the worker claims the key and sends. Leila gets one email, and the follow-up call and hand-off carry on as normal.

Does a person step in? Only if it keeps failing: a queue backlog that stays high raises an alert.

Document AI for legal and medical files

Smart Advocate

Document AI for legal and medical case files, shipped into a platform used by 20,000+ legal professionals.

Read the retrieval approach

My part: I led its document AI, from OCR, classification and structured extraction to knowledge-graph retrieval.

Measured with a GPT-4-class LLM judge, BLEU and ROUGE for answer text, and precision@k and recall@k for retrieval.

Senior AI Lead Engineer on Smart Advocate, at Devsinc

Work with me on document AI

Illustrative example · invented case file

QuestionWhen did the claimant's back pain start?

AnswerThe two records disagree. The intake note says 3 March1; in deposition, the claimant says the end of April2.

Choose a source to see its excerpt.

Intake note p. 3

Patient reports lower back pain since 3 March, after a fall at work.

Deposition 1 p. 112

Q. When did the pain start? A. Around the end of April, I think.

Demos and experiments

Watch the demo films
  • Browser simulation

    Automation Studio. Four agent workflow demos you can run in your browser.

    Try them (opens a new tab)
  • In development

    Mentanima. A legal copilot I'm building. Every answer cites its source, or says it can't answer.

  • Open source

    toon-mcp-server. Open source on PyPI. Converts JSON and system prompts to TOON.

    PyPI (opens a new tab)

Engineering notes

All writing

How I work

Three decisions from the systems above: the problem, the choice, and what it led to.

  1. Make redelivery harmless

    Problem
    Pub/Sub delivers every message at least once, so a crashed worker could email the same lead twice.
    Choice
    Each send carries an idempotency key, the lead ID plus the channel, claimed in a Firestore transaction before anything goes out.
    Consequence
    A redelivered message finds its key and is skipped, and the keys prevented duplicate sends.
    From the Pub/Sub write-up
  2. Wait instead of fail

    Problem
    Fifty enrichment and thirty personalization workers sharing the same LLM APIs hit rate limits within minutes.
    Choice
    A shared token bucket per API key in Redis, sized just under the paid quota. Workers wait and back off rather than fail.
    Consequence
    A busy minute slows the pipeline down instead of pushing messages toward the dead-letter queue.
    From the Pub/Sub write-up
  3. Measure, and say what it doesn't prove

    Problem
    Standard RAG struggled with legal questions whose answers span several documents.
    Choice
    A knowledge graph with a fine-tuned model, scored by an LLM judge, BLEU and ROUGE, and precision@k and recall@k, read together.
    Consequence
    A 40% accuracy gain over standard RAG. Both changed at once, so it doesn't isolate the graph's share, and the write-up says so.
    From the KAG write-up

Work with me

6+ years shipping software, since March 2020. AI-focused since April 2021. Independent contractor since July 2026.

Earlier, at Turing, I led a team producing RLHF training data for a frontier AI lab.

  1. Jul 2026 – nowIndependent contractorFreelance AI Engineer, remote
  2. Nov 2024 – Jun 2026DevsincSenior Software Engineer, full-time
  3. Mar 2022 – Nov 2024Amrood LabsAssociate Software Engineer, full-time
  4. Jan 2022 – Aug 2023ArbisoftDjango Developer, part-time
  5. Apr 2021 – Feb 2022TuringSoftware Engineer, POD lead, full-time, remote
  6. Mar 2020 – Mar 20212ndPlaceFounder and lead engineer

BS Computer Science, COMSATS University Islamabad

Ask this page

Ask a question about Hasnain. The answer quotes lines from this page and shows you where they are.

Talk to the human.

At Speculo.ai, qualified leads went to a person. So do you.

Hiring for a role, or bringing a project? The form asks which.

Message me

What is this about?

I read and answer these myself.

Or email codingwithhasnain@gmail.com